1.不同进制数的输出
#include
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
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
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
#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
#includeusing 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
#includeusing 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
#includeusing 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; } --------------------- 作者:✨38°du6毒 来源:CSDN 原文:https://blog.csdn.net/qq_36662459/article/details/88619523 版权声明:本文为博主原创文章,转载请附上博文链接!