C++ 文件操作之修改文件某一行数据
C++ 文件操作之修改文件某一行数据
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();
}
void main()
{
ModifyLineData("./123456.txt", 2, "国服老虎在线收徒!");
}