读写文件:文件流
头文件<fstream>
ifstream
ofstream
fstream
//输入control+Z退出判断
if(cin.eof()){
}
sscanf函数:用于从字符串按格式读取输入并存储到变量
getline函数:读取包含空格的一行字符串,头文件为#include<string>
//从文件中按格式读取
ifstream infile;
string line;
char name[32];
int age;
infile.open("user.txt");
infile.is_open();//状态位检查
while(1){
getline(infile, line);
if(infile.eof()){
break;
}
sscanf(line.str(), "姓名:%s 年龄:%d", name, &age);
}
infile.close();
seekg:定位到文件流位置
ifstram infile;
infile.seekg(-15, infile.end);
tellg: 返回当前位置距离文件开头的字节数
infile.tellg();
seekp: 定位到输出流位置
ofstream outfile;
outfile.seekp(4, outfile.beg);//定位到写入文件的第四个字节