#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"請輸入壹個整型數:";
cin>>num;
cout<<"八進制數是:"<<oct<<num<<endl;
cout<<"十進制數是:"<<dec<<num<<endl;
cout<<"十六進制數是:"<<hex<<num<<endl;
return 0;
}
2.get函數和getline函數
#include<iostream>
using namespace std;
int main()
{
char s1,s2,s3[50],s4[50];
cout<<"請輸入壹個字符:";
cout<<cin.get()<<endl;//讀取壹個字符的ASCII碼值
cin.get();//提取換行符
cout<<"請輸入兩個字符:";
cin.get(s1).get(s2);
cout<<s1<<s2<<endl;
cin.get();
cout<<"請輸入壹個字符串:";
cin.get(s3,50);
cout<<s3<<endl;
cin.get();
cout<<"請輸入壹個字符串:";
cin.getline(s4,50);
cout<<s4<<endl;
return 0;
}
3.read函數
#include<iostream>
using namespace std;
int main()
{
char ch[100];
char *str=ch;
cout<<"read 函數的使用:"<<endl;
cout<<"請輸入字符:"<<endl;
cin.read(str,100);
str[cin.gcount()]='\0';//這壹句要有,否則會出現亂碼
cout<<str<<endl;
}
4.put函數和write函數應用
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s1[100],s2[50]="Happy new year";
cout<<"put 和 writer函數的應用!"<<endl;
cout<<'M'<<endl;
cout.put('M');
cout.put('\n');
cout<<"請輸入壹串字符:";
cin.read(s1,100);
cout.write(s1,5)<<endl;
cout.write(s2,strlen(s2))<<endl;
return 0;
}
5.文件輸出流的應用
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
ofstream myFout("F:\\倩倩\\c++程序\\example.txt.txt",ios::out);
cout<<"文件輸出流的應用!"<<endl;
if(myFout.fail())
{
cout<<"This file dose not exist!"<<endl;
return 0;
}
else
{
cout<<"Open this file!"<<endl;
}
for(int i=0;i<4;i++)
{
cin>>str;
myFout<<str<<endl;
}
myFout.close();
return 0;
}
6.文件輸入流的應用
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
ifstream myFin("F:\\倩倩\\c++程序\\example.txt.txt",ios::in);
cout<<"文件輸入流的應用!";
if(myFin.fail())
{
cout<<"This file does not exist!"<<endl;
return 0;
}
else
{
cout<<"Open the file!"<<endl;
}
for(int i=0;i<4;i++)
{
myFin>>str;
cout<<str<<endl;
}
myFin.close();
return 0;
}
7.將FileRead文件內容寫到FileWrite文件
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str;
fstream FileRead,FileWrite;
FileRead.open("F:\\倩倩\\c++程序\\example.txt.txt",ios::in);
FileWrite.open("F:\\倩倩\\c++程序\\FileWrite.txt",ios::out);
while(!FileRead.eof())//判斷是否讀完文件
{
FileRead.read(&str,1);
cout<<str;
FileWrite.write(&str,1);
}
FileRead.close();
FileWrite.close();
return 0;
}