【发布时间】:2015-05-03 04:34:39
【问题描述】:
现在我可以读取文件的特定行,但问题是,如何在文件的同一特定行上写入?例如,假设第 1 行说“Test”,第 2 行说“Congratz”,第 3 行“Yay”,我怎样才能让它像 2 一样用“Yess”或表格中的东西替换。顺便说一句,在我的 int main() 代码中,我调用 readLine(0);并检查行是否为零,我调用 if (lineNumber == 0);提前致谢!
void readLine(int lineNum) {
ifstream fin("StartupMap.dat");
string s;
long length;
fin.seekg (0, ios::beg); // go to the first line
for (int i = 0; i <= lineNumber; i++) // loop 'till the desired line
getline(fin, s);
if (lineNumber == 0)
true1 = true;
length = fin.tellg(); // tell the first position at the line, maybe +1 or -1. try if it's not working
fin.seekg(length);
lineNumber = lineNum;
}
【问题讨论】:
-
除非你提前准备好行来保存额外的数据(即你在输出中放置填充物,否则你可能会在那个位置写更大的东西,但不会大于填充物) ,如果不重写文件的其余部分,您将无法做到这一点,行号并不重要。就文件系统而言,它们都只是字节。
标签: c++ visual-studio-2010 text document