Saturday 24 March 2012

The fopen and fclose Functions in C & C++


The cstdio library provides two functions for opening and closing a text file: fopen, fclose. Both functions use a FILE type pointer variable. The FILE type is defined in the library.

The fopen function requires a filename and a tag to specify the mode of access (read, write, append etc.) It returns a handle for accessing the file. The fclose function closes the file associated with the handle.

The first step is to create a pointer of the FILE type.

FILE * pFileHandle;

You open a text file for reading with this code:

pFileHandle = fopen("Filename", "r");

If the file does not exist, the functions returns NULL, otherwise it holds a handle for the opened file.

The file mode, “w” opens a file for writing. If the file does not exist, it is created. If the file exists, any existing data is overwritten.

pFileHandle = fopen("Filename", "w");

Finally, you can use the “a’ tag to append to a file. If the file does not exist, it is created. If the file exists, data is added to the end of the file.

pFileHandle = fopen("Filename", "a");

While it’s possible to close a file this way,

fclose(pFileHandle);

You should confirm the close function worked by analyzing the return value such as:

int iReturn = 0;
iReturn = fclose(pFileHandle);

If the return value is EOF, there was an error closing the file, otherwise the file was closed.

 
Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("File Open, File Close.\n");
     printf("For text files.\n\n");

     // Pointer to the file.
     FILE* pFileHandle;

     // Puts the file in the project path.
     char Filename[] = "MyFile.txt";

     // Version 1.
     // r attribute for reading the file.
     // Fails to open if the file does not exist.
     printf("File attribute: r\n");
     pFileHandle = fopen(Filename, "r");
     if (pFileHandle != NULL)
           printf("File exists.\n");
     else
           printf("File doesn't exist.\n");
     printf("File Handle: %X \n\n", pFileHandle);

     // Version 2.
     // w attribute for writing to the file.
     // If the file does not exist, it is created.
     // Any existing file content is overwritten.
     printf("File attribute: w\n");
     pFileHandle = fopen(Filename, "w");
     if (pFileHandle != NULL)
           printf("File exists.\n");
     else
          printf("File doesn't exist.\n");
     printf("File Handle: %X \n\n", pFileHandle);

     // Version 3.
     // a attribute for appending to the file.
     // If the file does not exist, it is created.
     // Any existing file content remains.
     printf("File attribute: a\n");
     pFileHandle = fopen(Filename, "a");
     if (pFileHandle != NULL)
           printf("File exists.\n");
     else
           printf("File doesn't exist.\n");
     printf("File Handle: %X \n\n", pFileHandle);

     // Close the file.
     int iReturn = 0;
     iReturn = fclose(pFileHandle);
     // iReturn = 0 or false, means success closing the file.
     // iReturn = EOF means an error.
     if (iReturn == EOF)
           printf("Error closing the file.\n");
     else
           printf("File closed.\n");
     printf("File Handle: %X \n\n", pFileHandle);

     // Return some value.
     return 0;
} // end main


Output.

No comments:

Post a Comment