If I am fread()'ing through a file and want to append it when i find a particular item, can i just do an fwrite() when i find it or do i have to first fseek() to that location and then do an fwrite()?

Recommended Answers

All 2 Replies

You do need to do an fseek to that location, but not because C uses separate read and write pointers. The standard says that when switching between read and write mode on a stream, there needs to be a flush or seek between them. So you would read to the location you want to write, then do fseek(strm, 0, SEEK_CUR); to switch the mode without changing the file position.

And keep in mind that you will be overwriting whatever is there, not inserting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.