Roy Gavrielov
Roy Gavrielov

Reputation: 607

How can i write something to file at the end of the file in c++

i'm writing something to file and it writes it in the middle of the file, is there any function that writes output to the end of the file? thanx in advance. ok this is really wierd i'm running with the visual studio debugger and i see that it writes thing to file like this : A B C D which is good, and than when i'm writing something for example E it writes it here A B E C D this is really wierd, how can i fix it?

Upvotes: 1

Views: 179

Answers (2)

phihag
phihag

Reputation: 288270

With the plain old C functions, open the file with O_APPEND or call lseek(fd, 0, SEEK_END) before writing.

With ofstream, call file.seekp(0, ios_base::end) before writing.

Upvotes: 2

CB Bailey
CB Bailey

Reputation: 793119

If you use std::ofstream and open the file in append mode (using std::ios_base::app in the mode mask) then all writes will be made at the end of the file.

Upvotes: 3

Related Questions