文件修改匹配行
a.txt
111111111
222222222
333333333
#include <iostream>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
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 += string(lineData);
strFileData += "\n";
}
else
{
strFileData += string(tmpLineData);
strFileData += "\n";
}
line++;
}
in.close();
//写入文件
ofstream out;
out.open(fileName);
out.flush();
out<<strFileData;
out.close();
}
int main()
{
ModifyLineData("./a.txt", 2, "hhhhhhh");
return 0;
}
执行完函数后的a.txt文件