开发者

Textfiles C++ Editing the very first line

Example of Textfile:

5 <- I need to edit this number.

0

1

0

6

(Sample Code Not Whole Program)

#include <fstream>
#include <iostream>
using namespace std;
int main() { 
int i;
cin>>i;
    std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app);
    file开发者_StackOverflow.seekp(0);

    file << i;
    return 0;
}

With this code the number is added here: (example.txt)

5

0

1

0

67 <<

Please note that from the bottom the numbers will keep increasing so it has to be always the first line not that specific 5.

Please Help Thanks


You have opened the file in a mode that requests that all new data is appended to the end of the file (std::ios::app). Don't specify that flag if you don't want to always append.

Note that you will encounter problems if the new line you're writing is not exactly the same length as the existing line. In the case where it's a different length, you will have to copy and rewrite the entire remainder of the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜