在文件行的开头设置seekp()

时间:2014-11-13 16:27:39

标签: c++ fstream

我想替换同一文件中的一行,即重写文件。目前,其写下线的行有待改变。我正在使用 seekp()来定位必须写入替换行的位置。以下是代码段。

std::fstream file (file_path, std::ios_base::in | std::ios_base::out);

 while (getline (file, line)){

pos = file.tellp();

file.seekp(pos, std::ios_base::beg);

// calls another function which find some variables in line, change them and return String 

const char *c = strdup(line.c_str());

file.write(c,line.length());
}

一切正常。唯一的问题是它在原始行下的文件中编写新的。可能是设置 tellp() seekp()的问题。有什么方法可以将它们设置为行的开头,用新的onE替换原始行?

谢谢!

0 个答案:

没有答案
相关问题