When a file that already exists is opened in append mode the files existing contents are not erased?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In C, fopen() is used to open a file in different modes. To open a file in write mode, “w” is specified.  When mode “w” is specified, it creates an empty file for output operations. What if the file already exists? If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file. For example, in the following program, if “test.txt” already exists, its contents are removed and “GeeksforGeeks” is written to it. 

    C++

    #include <iostream>

    #include <cstdlib>

    int main()

    {

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

        if (fp == NULL)

        {

            puts("Couldn't open file");

            exit(0);

        }

        else

        {

            fputs("GeeksforGeeks", fp);

            puts("Done");

            fclose(fp);

        }

        return 0;

    C

    #include <stdio.h>

    #include <stdlib.h>

    int main()

    {

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

        if (fp == NULL)

        {

            puts("Couldn't open file");

            exit(0);

        }

        else

        {

            fputs("GeeksforGeeks", fp);

            puts("Done");

            fclose(fp);

        }

        return 0;

    }

    The above behavior may lead to unexpected results. If programmer’s intention was to create a new file and a file with same name already exists, the existing file’s contents are overwritten. The latest C standard C11 provides a new mode “x” which is exclusive create-and-open mode. Mode “x” can be used with any “w” specifier, like “wx”, “wbx”. When x is used with w, fopen() returns NULL if file already exists or could not open. Following is modified C11 program that doesn’t overwrite an existing file. 

    C++

    #include <iostream>

    #include <cstdlib>

    int main()

    {

        FILE *fp = fopen("test.txt", "wx");

        if (fp == NULL)

        {

            puts("Couldn't open file or file already exists");

            exit(0);

        }

        else

        {

            fputs("GeeksforGeeks", fp);

            puts("Done");

            fclose(fp);

        }

        return 0;

    C

    #include <stdio.h>

    #include <stdlib.h>

    int main()

    {

        FILE *fp = fopen("test.txt", "wx");

        if (fp == NULL)

        {

            puts("Couldn't open file or file already exists");

            exit(0);

        }

        else

        {

            fputs("GeeksforGeeks", fp);

            puts("Done");

            fclose(fp);

        }

        return 0;

    }

    References: Do not make assumptions about fopen() and file creation http://en.wikipedia.org/wiki/C11_(C_standard_revision) http://www.cplusplus.com/reference/cstdio/freopen/ This article is compiled by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


    When a file that already exists is opened in append mode the file's existing contents are not erased?

    If an existing file is opened in append mode, what happens to the file's existing contents? It will not be erased and new data will be written at the end of the file's current contents.

    What happens if you open an output file and the file already exists?

    In most languages, when you open an output file and that file already exists on the disk, the contents of the existing file will be erased. When an input file is opened, its read position is initially set to the first item in the file.

    What will happen when a program opens a file in write mode if the file doesn't exist?

    If you open a file for writing and the file doesn't exist, then the file is created with 0 length.

    What type of file access jumps directly to any piece of data in the file without reading the data that came before it?

    When working with a sequential access file, you can jump directly to any piece of data in the file without reading the data that comes before it.