C++一周学习总结(2021/03/28)

这篇博客详细介绍了C++中如何进行文件操作,包括读写文件、二进制文件处理以及文件流的状态检查。通过示例代码展示了如何打开、写入和读取文本及二进制文件,并讲解了文件流的定位方法。同时,还涵盖了文件流状态的检查方法,如is_open()、eof()、fail()等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一周主要学习了关于C++的文件的相关内容
io类

读写文件

需包含头文件 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值