一周主要学习了关于C++的文件的相关内容
读写文件
需包含头文件 fstream
类库:
ifstream 对文件输入(读文件)
ofstream 对文件输出(写文件)
对文本文件进行读写
文件打开方式:
模式标志 | 描述 |
---|---|
ios::in | 读方式打开文件 |
ios::out | 写方式打开文件 |
ios::trunc | 如果文件已存在,就会把文件长度截断为0 |
ios::app | 尾部追加的方式(在文件尾写入) |
ios::ate | 文件打开后,定位到文件尾 |
ios::binary | 二进制方式(默认为文件方式) |
[注] 以上方式之间可以用位运算符 | 进行结合使用
1. 写文件
从控制台输入内容,保存到文件中
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string name;
int age;
ofstream outfile; //创建输出流对象outfile
outfile.open("text.txt"); //打开文件,若不存在这个文件,则会先创建这个文件
//使用输出流对象用写的方式打开文件,以截断的方式打开
// outfile.open("text.txt",ios::out | ios::trunc);
if(!outfile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(1){
cout << "请输入姓名[输入ctrl+z结束]:" << endl;
cin >> name;
if(cin.eof()){
break;
}
outfile << name << "\t";
cout << "请输入年龄:" << endl;
cin >> age;
outfile << age << "\n";
}
outfile.close();
return 0;
}
2. 读文件
从文件中将内容读出来,显示在控制台
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string name;
int age;
ifstream infile; //创建输出流对象infile
infile.open("text.txt"); //打开文件
//使用输出流对象用读的方式打开文件
// infile.open("text.txt",ios::in);
if(!infile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(1){
infile >> name;
if(infile.eof(){ //判断文件是否读完
break;
}
cout << name << "\t";
infile >> age;
cout << age << endl;
}
infile.close();
return 0;
}
对二进制文件进行读写
1. 写文件
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string name;
int age;
ofstream outfile; //创建输出流对象outfile
//使用输出流对象用写的方式打开文件,以二进制文件的方式打开
outfile.open("text.txt",ios::out | ios::binary | ios::trunc);
if(!outfile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(1){
cout << "请输入姓名[输入ctrl+z结束]:" << endl;
cin >> name;
if(cin.eof()){
break;
}
outfile << name << "\t";
cout << "请输入年龄:" << endl;
cin >> age;
// outfile << age << "\n"; //会自动转换成文件方式写入,导致乱码
outfile.write((char*)&age,sizeof(age));
}
outfile.close();
return 0;
}
2. 读文件
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string name;
int age;
ifstream infile; //创建输出流对象infile
//使用输出流对象用读的方式打开文件,以二进制文件的方式打开
infile.open("text.txt",ios::in | ios::binary);
if(!infile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(1){
infile >> name;
if(infile.eof()){
break;
}
cout << name << "\t";
//因为写入的时候,name后加了\t,因此多读一个字节跳过制表符
char pass;
infile.read(&pass,sizeof(pass));
infile.read((char*)&age,sizeof(age));
cout << age << endl;
}
infile.close();
return 0;
}
对指定格式文件进行读写
对stringstream类型的内容进行读写
1. 写文件
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespce std;
int main(){
string name;
int age;
ofstream outfile; //创建输出流对象outfile
outfile.open("text.txt");
if(!outfile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(1){
cout << "请输入姓名[输入ctrl+z结束]:" << endl;
cin >> name;
if(cin.eof()){
break;
}
cout << "请输入年龄:" << endl;
cin >> age;
stringstream ret;
ret << "姓名:" << name << "\t\t年龄" << age << endl;
outfile << ret.str();
}
outfile.close();
return 0;
}
2. 读文件
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
char name[16];
int age;
ifstream infile; //创建输出流对象infile
string line;
infile.open("text.txt");
if(!infile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(1){
getline(infile,line);
sscanf_s(line.c_str(),"姓名:%d 年龄",name,sizeof(name),&age);
cout << name << "\t" << age << endl;
}
infile.close();
return 0;
}
文件流的状态检查
方法 | 描述 |
---|---|
is_open() | 文件流是否打开成功 |
eof() | 流(stream)是否结束 |
fail() | 流的failbit或者badbit被置位时, 返回true |
bad() | 流的badbit置位时, 返回true |
good() | 流处于有效状态时, 返回true |
clear() | 流s的所有状态都被复位 |
文件流的定位
1. seekg
seekg(off_type offset, ios::seekdir origin)
作用:设置输出流的位置
第一个参数是偏移量,第二个参数是起始位置(beg/cur/end)
beg:相对于文件开始的位置
cur:相对于文件的当前位置
end:相对于文件的结束位置
//读取当前文件的最后50个字符
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string out;
ifstream infile; //创建输出流对象infile
infile.open("text.txt"); //打开文件
inflie.seekg(-50,infile.end); //文件指针移动到文件的倒数第五十个字符处
if(!infile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
while(!infile.eof()){
string line;
getline(inflie,line);
cout << line << endl;
}
infile.close();
return 0;
}
2. tellg
返回该输入流当前位置(距文件的起始位置的偏移量)
//获取文件的长度
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string out;
ifstream infile; //创建输出流对象infile
infile.open("text.txt"); //打开文件
if(!infile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
inflie.seekg(0,infile.end); //文件指针移动到文件的最后
auto len = inflie.tellg();
cout << "文件长度为:" << len << endl;
infile.close();
return 0;
}
3. seekp
设置输出流的位置
seekp(off_type offset, ios::seekdir origin)
第一个参数是偏移量,第二个参数是起始位置(beg/cur/end)
beg:相对于文件开始的位置
cur:相对于文件的当前位置
end:相对于文件的结束位置
//先向文件写入12345678,再在第四个位置写入ABC
#include <iostream>
#include <fstream>
#include <string>
using namespce std;
int main(){
string out;
ofstream outfile; //创建输出流对象outfile
outfile.open("text.txt"); //打开文件
if(!outfile.is_open()){ //判断文件是否打开成功
cout << "文件打开失败" << endl;
return 1;
}
outfile << "12345678";
outfile.seekp(4,outfile.beg);
outfile << "ABC";
outfile.close();
return 0;
}