-4

i am new to c i finished all the fundamentales and now i am studying the file header stdio.h and found w and w+

#include <stdio.h>

int main(){
    FILE *file = fopen("test.txt", "w+");

    fprintf(file, "hello world");


    char buffer[992];
    fscanf(file, "%s", buffer);
    printf("%s", buffer);

}

confusing because they both erase the file even before reading i don't get the point of using them where is the difference ?

01a
  • 1
  • Did you look at [the documentation](https://en.cppreference.com/w/c/io/fopen)? – Kevin Sep 03 '23 at 00:03
  • yes it is not useful – 01a Sep 03 '23 at 00:04
  • but the problem is fixed now because we have to rewind the pointer – 01a Sep 03 '23 at 00:04
  • In what way? It pretty clearly states what the `+` means – Kevin Sep 03 '23 at 00:04
  • + means nothing there i just needed to rewind the pointer because the pointer stayed in the end of the write that is what causes the issue – 01a Sep 03 '23 at 00:07
  • `+` Means the file is open for both reading and writing, and certain operations need to be done between reads and writes to be valid. `w` means the existing file contents are erased. – Kevin Sep 03 '23 at 00:08
  • `w --> O_WRONLY | O_TRUNC | O_CREAT` and `w+ --> O_RDWR | O_TRUNC | O_CREAT` – Craig Estey Sep 03 '23 at 00:14
  • you guys making no sense at all – 01a Sep 03 '23 at 00:20
  • the problem is in the pointer c doesn't reset the file pointer so that is the issue i just needed to reset it manually by using rewind() function – 01a Sep 03 '23 at 00:21
  • You're going to have to explain your confusion to us. You asked about the difference between `w` and `w+`. The documentation is pretty clear on that difference, and also how to interleave reads and writes when using `r+`/`w+`. – Kevin Sep 03 '23 at 00:23
  • it is not clear the problem was just the ``pointer`` c never reset the file pointer – 01a Sep 03 '23 at 00:28
  • Because, as the documentation says, "In update mode (`'+'`), both input and output may be performed, but output cannot be followed by input without an intervening call to `fflush`, `fseek`, `fsetpos` or `rewind`...". Seems clear to me – Kevin Sep 03 '23 at 00:31
  • Were you perhaps expecting the pointer to automatically rewind after a write? This would not be ideal behaviour if multiple writes are being made on the same file/stream. – HorusKol Sep 03 '23 at 00:41

2 Answers2

1

The difference between w and w+ is that w truncates the file to zero size and allows only writing to the file, while w+ truncates the file to zero and allows both reading and writing.

For example this is allowed:

FILE *file = fopen("test.txt", "w+");

fprintf(file, "hello world");
rewind(file);

char buffer[992];

fscanf(file, "%s", buffer);
printf("%s", buffer);

But this is not::

FILE *file = fopen("test.txt", "w");

fprintf(file, "hello world");
rewind(file);

char buffer[992];

fscanf(file, "%s", buffer);    // error, reading not allowed
printf("%s", buffer);
dbush
  • 205,898
  • 23
  • 218
  • 273
0

Phrasing the documentation a bit differently:

w creates a file, or opens an existing file and erase it's content, and only allows you to write into that file.

w+ creates a file, or opens an existing file and erase it's content, and allows you to read and write from the same stream (you don't have to write, close and reopen the file to read from it). However, you will need to rewind the pointer to read what you have written in there.

You can rewind the pointer on a file you opened as w - but attempting to then read from that stream will cause an error.

HorusKol
  • 8,375
  • 10
  • 51
  • 92