stdio question
Hi all,
Does fprintf overwrite text that is already present in the file? It that behaviour dependent on how I opened the file? I'm opening with "+w" in this case.
I can get a file-position-pointer with
fpos_t pos;
fgetpos(fileptr, &pos);
so that's ok, but what exactly is happening to my "access point" to the file when I repeatedly
fscanf(fileptr, "\n");
Does that take my pos to the beginning of a newline each time it's called? If not, how can I do that?
The context of this problem is the following: I want to modify some data in the middle of the file, so I search for a keyword indicating the location of my data and grab the pos. Then I make some changes to the data right after that point using fprintf. Once I'm done that, I need to the the file pointer down to the bottom of the file so I can append some closing tags.
When I modify the data in the middle of the file, say with something like
fprintf(fileptr, "%s\n", somestring);
Does that push the next lines down and allow me to insert a line, or does it overwrite the existing line?
As I'm sure you're thinking, I could really use a good tutorial on stdio. I've been finding the reference on msdn and cplusplus.com a bit wanting.
Thanks for your help
Quote:Original post by robot ears
Does fprintf overwrite text that is already present in the file? It that behaviour dependent on how I opened the file? I'm opening with "+w" in this case.
Yes, it depends on how you opened the file. Mode "w+" means read-write, but also destroy the contents of the file if it exists. "a+" on the other hand will preserve the file contents and let you start appending to the end of the file (or create a new one if it doesn't exist).
Quote:I can get a file-position-pointer with
fpos_t pos;
fgetpos(fileptr, &pos);
so that's ok, but what exactly is happening to my "access point" to the file when I repeatedly
fscanf(fileptr, "\n");
Does that take my pos to the beginning of a newline each time it's called? If not, how can I do that?
The context of this problem is the following: I want to modify some data in the middle of the file, so I search for a keyword indicating the location of my data and grab the pos. Then I make some changes to the data right after that point using fprintf. Once I'm done that, I need to the the file pointer down to the bottom of the file so I can append some closing tags.
When I modify the data in the middle of the file, say with something like
fprintf(fileptr, "%s\n", somestring);
Does that push the next lines down and allow me to insert a line, or does it overwrite the existing line?
As I'm sure you're thinking, I could really use a good tutorial on stdio. I've been finding the reference on msdn and cplusplus.com a bit wanting.
Actually, MSDN says the following:
Quote:printf
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
So you can't do what you want with file operations alone, at least not easily. You need to read in the contents of the file, make any modifications in memory, and then write out the contents again.
If you can use C++, and are working with text data, then loading the file into a std::string will give you access to some basic find/replace functionality. In pure C it's going to be a bit more painstaking. You can probably Google "c string library" and see if there are any libraries that can help you out.
The standard c file io routines will assume you know all the formatting you want to do.
fprintf( fileptr, "%s\n", somestring );
will overwrite strlen(somestring)+(1 or 2) bytes of the file. It doesn't care what was there before.
What you have to do is copy all the data down far enough for your insert, then insert.
I second Zipster's faster posting, and suggest you use C++ if possible.
Some keywords to get you started:
std::getline()
std::vector< std::string > content;
std::fstream file();
You'd getline everything into the content vector, find your line, insert some new lines, then write everything back out.
fprintf( fileptr, "%s\n", somestring );
will overwrite strlen(somestring)+(1 or 2) bytes of the file. It doesn't care what was there before.
What you have to do is copy all the data down far enough for your insert, then insert.
I second Zipster's faster posting, and suggest you use C++ if possible.
Some keywords to get you started:
std::getline()
std::vector< std::string > content;
std::fstream file();
You'd getline everything into the content vector, find your line, insert some new lines, then write everything back out.
For small, non-critical files you can simply read the entire file into memory, make any edits and then write it out in a different step.
For critical files you would write to a new, temporary file. You would then use an atomic move operation to rename the temporary file to the target file. This way no data is lost if your program crashes or the machine halts while outputting.
For critical files you would write to a new, temporary file. You would then use an atomic move operation to rename the temporary file to the target file. This way no data is lost if your program crashes or the machine halts while outputting.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement