开发者

When saving to a file is there a way to not override whats already in the file?

开发者 https://www.devze.com 2023-01-19 18:03 出处:网络
When saving something to a text file is there a way to not override whats in the file already? EX. blah.txt

When saving something to a text file is there a way to not override whats in the file already?

EX.

blah.txt

this is an example

fout << "of saving to a file.";
开发者_如何学编程

i want the "of saving to a file" to be added on to "this is an example" not to override it.


use the append flag, like this:

fstream fout("blah.txt", ios::app);

if you are opening it after declaration, use something like this:

fout.open("blash.txt", fstream::app);


You should open the file with the appropriate write mode in order to append to it instead of overwriting:

pFile = fopen ("myfile.txt","a");

(C style), or

fstream filestr ("myfile.txt", fstream::ate | fstream::out);

(C++ style). In the latter case, you may want to use fstream::app instead of fstream::ate - this sets the file pointer to the end of the file before each output operation.


Yes. You need to open the file in append mode.

ofstream fout("blah.txt", fstream::out | fstream::app);


You need to open the file output stream in append mode:

ofstream fout(<path>, ios::app);


You can manually set the write position if you want to

u need: #include <stdio.h> ...

FILE *file = fopen("my_file.txt", "w");
fputs ("This is some text." , file);
fseek (file , 15, SEEK_SET);
fputs (" sam" ,file);
fclose (file);
0

精彩评论

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