How to Overwrite Only Part of a File in C++

C - How to search a file and overwrite a portion of it

You can write a loop to read the records into a record structure, one at a time. After each read, you can compare the record just read with the desired record, reading the next record if it is not the one you need.

You've just found the record you wanted, but you've also just read beyond the record; you have to seek back to overwrite the record. You also have to seek between read and write or vice versa, every time, because the C standard says the behaviour is undefined if you don't. That means a seek before the write, and another seek before going back for the next record.

Since you've opened the file in "ab" mode, any seeking is immaterial since all writes will be appended at the end. You probably want "r+b" as the mode to fopen().

See fseek() goes back to the end of file when writing a value for a recent discussion of the same general issues.

How to overwrite only part of a file in c++

If the replacement string is the same length, you can make the change in place. If the replacement string is shorter, you may be able to pad it with zero-width spaces or similar to make it the same number of bytes, and make the change in place. If the replacement string is longer, there just isn't enough room unless you first move all remaining data.

Overwrite only a specific part of a file

If the new guest names do not match exactly the length of the old guest names, you can't perform the replacement in-place, it's best to use a temporary file.

  • Read the original file line-by-line until you locate the "Guest list:" delimiter copying each line to the temporary file.
  • Write the new guest list to the temporary file.
  • Read lines from the original file until you locate the "Table list:" delimiter.
  • Read the original file line-by-line until its end, copying each line to the temporary file.
  • Delete the original file. (Or rename it adding a backup extension to its name.)
  • Rename the temporary file to the name of the original file.

How to read and overwrite text file in C?

long pos = ftell(fp);//Save the current position
while (fgets(buffer, 500, fp) != NULL) {
buffer[0] = 'x';
fseek(fp, pos, SEEK_SET);//move to beginning of line
fprintf(fp, "%s", buffer);
fflush(fp);
pos = ftell(fp);//Save the current position
}

How to overwrite a file in C?

You decide that in fopen. Just use "w" or "w+" instead of "r+".

Overwrite a line in a file

I could reproduce and fix.

The present problem is that when you open a file in r+ you must call fseek at each time you switch from reading to writing and from writing to reading.

Here, you correctly call fseek before writing the 0, but not after that write and the following read. The file pointer is not correctly positionned and you get undefined behaviour.

Fix is trivial, simply replace :

if(times == 3)
{
fseek(f, pos, SEEK_SET);
fprintf(f, "%lu\n", 0);
}

with

if(times == 3)
{
fseek(f, pos, SEEK_SET);
fprintf(f, "%lu\n", 0);
pos = ftell(f);
fseek(f, pos, SEEK_SET);
}

But BEWARE : it works here because you replace a line by a line of exactly same length. If you tried to replace a line containing 1000 with a line containing 0 you would get an extra line containing 0 on a windows system where end of line is \r\n and 00 on an unix like system with end of line \n.

Because here is what would happen (Windows case) :

Before rewrite :

...  1  0  0  0 \r \n ...

After :

...  0 \r \n  0 \r \n ...

because a sequential file is ... a sequential serie of byte !

Override part of text file

fopen(path,"w+") opens a file for write and update, deleting the file first if it already exists. To avoid deleting the file first, one can use fopen(path,"r+") to open it for reading and update.

If the aim is just to replace the specified characters, then the \n at the end of the fprintf format string should be omitted, so that the line reads i = fprintf (fp, "%.*s", bytes_number, data).

Trying not to overwrite a file in C?

Yes, it is possible. By opening it with r+ you open it for 'reading and writing' (while w opens it for writing freshly).

Regarding your other question: Open a file with w and write 1000 1024 byte blocks to the file like this:

FILE *fp = fopen("file", "wb");
if(fp) {
int i = 0;
char Buf[1024];
for(; i < 1000; ++i)
fwrite(Buf, 1, 1024, fp);
fclose(fp);
}

Just once more the fopen flags for you:

r -> Opens file for reading. (File remains unchanged)

w -> Opens file for writing. (File gets erased)

a -> Opens file for appending. (File remains unchanged, file pointer gets moved to end)

Aside from these three main types, you can add a few more additional options:

b -> Opens the file as binary, ignoring formatting characters like \n

t -> Opens the file as text, specifically parsing \n as \r\n under Windows.



Related Topics



Leave a reply



Submit