3

I don't understand what is the practical difference between r+ and a+ in fopen in c. Can someone help me?

Zaratruta
  • 1,261
  • 2
  • 14
  • 21

3 Answers3

5

r+ will open a file for reading and writing. It will fail if the file does not exist. fseek can be used to read and write anywhere in the file.

w+ will open a file for reading and writing. It will create the file if the file does not exist, and destroy and recreate the file if the file does exist. fseek can be used to read anywhere in the file.

a+ will open a file for reading and writing. It will create the file if the file does not exist. fseek can be used to read anywhere in the file, but writes will always append to the end of the file regardless of any calls to fseek.

contrapants
  • 735
  • 4
  • 16
3

Taken this from fopen man page

  • r Open text file for reading. The stream is positioned at the beginning of the file.

  • r+ Open for reading and writing. The stream is positioned at the beginning of the file.

  • w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

  • w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

  • a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

  • a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

I decided to give an example for a future reference.First of all FILE* fp will be a pointer to a stream, not the actual data of the file in the hard disk.So when fopen is invoked , it's invoked on a stream, not the file.Just keep in mind that one file can have multiple streams. What + is (b is also acceptable here, i.e. r+ == rb) here, is simply an 'update'.This has to do with the way that system writes in a file.A file can be written either if data erased and written as a whole, or change just the data that changed.For example if we need to update "Today is monday" to "TODAY is Monday" either we purge the sentence and write it again.But if we UPDATE we just change o to O, n to N,a to A,y to Y and m to M.

To the actual example, lets assume that we have an empty file and we call (I have the whole code,in case you want to take it and test your self)

int main() {
    FILE * fp;

    if(fp==NULL)
    {
        return -1;
    }

    fp = fopen("file.txt", "r+");
    fprintf(fp, "%s %s %s %d", "This", " is ", "test ticket ", 10001);
    fflush(fp);
    fclose(fp);

    fp = fopen("file.txt", "r+");
    fprintf(fp, "%s %s %s %d", "This", " is ", "test ticket ", 10002);
    fflush(fp);
    fclose(fp);

    fp = fopen("file.txt", "r+");
    fprintf(fp, "%s %s %s %d", "This", " is ", "test ticket ", 10003);
    fflush(fp);
    fclose(fp);

    return(0);
}

Also this question has already answered here

This will give us a file like:

This is test ticket 10003

which is normal because the first call wrote

This is test ticket 10001

the second call changed 1 to 2 and the third call changed 2 to 3. Now if we change those 3 fopen calls like:

    fp = fopen("file.txt", "a+");
    fprintf(fp, "%s %s %s %d", "This", " is ", "test ticket ", 10001);
    fflush(fp);
    fclose(fp);

    fp = fopen("file.txt", "a+");
    fprintf(fp, "%s %s %s %d", "This", " is ", "test ticket ", 10002);
    fflush(fp);
    fclose(fp);

    fp = fopen("file.txt", "a+");
    fprintf(fp, "%s %s %s %d", "This", " is ", "test ticket ", 10003);
    fflush(fp);
    fclose(fp);

We will get

This is test ticket 10001This is test ticket 10002This is test ticket 10003

as each call appended.

0

yet another reference www.cplusplus.com

  • "r" read: Open file for input operations. The file must exist.
  • "w" write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
  • "a" append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.
  • "r+" read/update: Open a file for update (both for input and output). The file must exist.
  • "w+" write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
  • "a+" append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.
static_cast
  • 1,118
  • 1
  • 15
  • 21