C++ 替换文本内的一行的数据

发布于 2020-07-02  86 次阅读


void ModifyLineData(char *fileName, int lineNum, char *lineData)
{
	ifstream in; 
	in.open(fileName);

	string strFileData = ""; 
	int line = 1;
	char tmpLineData[1024] = {0};
	while(in.getline(tmpLineData, sizeof(tmpLineData)))
	{   
		if (line == lineNum)
		{   
			strFileData += CharToStr(lineData);
			strFileData += '\n';
		}   
		else
		{   
			strFileData += CharToStr(tmpLineData);
			strFileData += '\n';
		}   
		line++;
	}   
	in.close();

	//写入文件
	ofstream out;
	out.open(fileName);
	out.flush();
	out << strFileData;
	out.close();
}