C++读取写入.txt文件(ifstream/ofstream)—读取指定行,修改指定行,复制文件,清除文件,统计文件行数

C++对txt文件的操作是基于fstream/ifstream/ofstream类的,使用时需添加头文件包含:

#include<fstream>
针对txt文件的操作大体包括基本的读写,读取指定行,修改指定行,复制文件,清除文件,统计文件行数等几种,分别实现如下;

以下重点在于实现几个操作,关于这一块的基础知识参考:C++文件读写详解(ofstream,ifstream,fstream)

1、统计txt文件行数;

/*
**统计txt文件行数
*/
int CountLines(string filename)
{
	ifstream ReadFile;
	int n = 0;
	string tmp;
	ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件  
	if (ReadFile.fail())//文件打开失败:返回0  
	{
		return 0;
	}
	else//文件存在  
	{
		while (getline(ReadFile, tmp, '\n'))
		{
			n++;
		}
		ReadFile.close();
		return n;
	}
}
2、复制txt文件;

/*
**复制txt文件
*/
void copyTxt(string srcFilename, string dstFilename)
{
	ifstream infile;
	ofstream outfile;
	string temp;
	infile.open(srcFilename, ios::in);
	outfile.open(dstFilename, ios::trunc | ios::out);
	if (infile.good())
	{
		while (!infile.eof())
		{
			getline(infile, temp, '\n');
			outfile << temp << '\n';
		}
	}
	infile.close();
	outfile.close();

}
3、清除txt文件;

/*
**清除txt文件
*/
void clearTxt(string filename)
{
	ofstream text;
	text.open(filename, ios::out | ios::trunc);//如果重新设置需要
	text.close();
}
4、修改指定行数据
/*
**修改指定行数据
*/
void ResetLine(string file,int line)
{	
	int total = CountLines(file);
	if (line > total || line < 1)
	{
		MessageBox(_T("修改超出配置文件行数范围"));
		return;
	}
	string bup = _T(".\\tmp.txt");//备份文件
	copyTxt(file,bup);
	ifstream rfile;
	ofstream wfile;
	rfile.open(bup,ios::in);
	wfile.open(file,ios::out|ios::trunc);

	string str;
	int i = 1;
	while (!rfile.eof())
	{		
		if (i == line)
		{
			CString strMFC;
			strMFC.Format(_T("%f %f %f\n"), m_pAssistCam, m_tAssistCam, m_zAssistCam);
			wfile << strMFC.GetBuffer(0);//写入修改内容
		}
		else
		{
			//rfile.getline()
			getline(rfile, str, '\n');
			wfile << str << '\n';
		}
		i++;
	}
	rfile.close();
	wfile.close();
}
5、读取指定行数据

/*
  **读取txt指定行数据存入string
  */
string readTxt(string filename, int line)
{
	//line行数限制 1 - lines
	ifstream text;
	text.open(filename, ios::in);

	vector<string> strVec;
	while (!text.eof())  //行0 - 行lines对应strvect[0] - strvect[lines]
	{
		string inbuf;
		getline(text, inbuf, '\n');
		strVec.push_back(inbuf);
	}
	return strVec[line - 1];
}





  • 22
    点赞
  • 204
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我来回答你的问题。 首先,你需要打开TXT文件读取其内容,可以使用C++的fstream库来实现。具体代码如下: ```c++ #include <fstream> #include <string> #include <vector> using namespace std; int main() { ifstream inFile("input.txt"); // 打开输入文件 string line; vector<string> lines; // 存储每内容的vector while (getline(inFile, line)) { lines.push_back(line); // 将每内容存储到vector中 } inFile.close(); // 关闭输入文件 ``` 接下来,你需要将每内容按照分隔符进分割,并将分割后的结果存储到两个不同的TXT文件中。可以使用C++的stringstream和getline函数来实现。具体代码如下: ```c++ ofstream outFile1("output1.txt"); // 打开输出文件1 ofstream outFile2("output2.txt"); // 打开输出文件2 for (const auto& line : lines) { stringstream ss(line); string token; getline(ss, token, ':'); // 以分隔符':'进分割 outFile1 << token << endl; // 将分割后的第一部分写入输出文件1 getline(ss, token, ':'); // 继续以分隔符':'进分割 outFile2 << token << endl; // 将分割后的第二部分写入输出文件2 } outFile1.close(); // 关闭输出文件1 outFile2.close(); // 关闭输出文件2 ``` 最后,记得在完成所有操作后关闭所有打开的文件。完整代码如下: ```c++ #include <fstream> #include <string> #include <vector> using namespace std; int main() { ifstream inFile("input.txt"); // 打开输入文件 string line; vector<string> lines; // 存储每内容的vector while (getline(inFile, line)) { lines.push_back(line); // 将每内容存储到vector中 } inFile.close(); // 关闭输入文件 ofstream outFile1("output1.txt"); // 打开输出文件1 ofstream outFile2("output2.txt"); // 打开输出文件2 for (const auto& line : lines) { stringstream ss(line); string token; getline(ss, token, ':'); // 以分隔符':'进分割 outFile1 << token << endl; // 将分割后的第一部分写入输出文件1 getline(ss, token, ':'); // 继续以分隔符':'进分割 outFile2 << token << endl; // 将分割后的第二部分写入输出文件2 } outFile1.close(); // 关闭输出文件1 outFile2.close(); // 关闭输出文件2 return 0; } ``` 希望我的回答能够帮助到你!如果你还有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值