C file input/output

This is an old revision of this page, as edited by Mwtoews (talk | contribs) at 09:13, 1 February 2007 (merged fopen and fclose). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In C programming, file input and output is controlled by the the fopen and fclose functions, respectively, which are defined in the stdio.h C standard library. Typically, they are used in a sequence:

  1. A file is opened for reading/writing/appending, using fopen;
  2. The file is processed;
  3. The file is closed, using fclose.

fopen

fopen returns an I/O stream attached to the specified file or other device from which reading and writing can be done. If the function fails, it returns 0. Because the functionality is so useful, many languages derived from C provide functions of the same name, with the same or similar function: for example, PHP. fopen is considered higher-level than the open system call of UNIX operating systems. The related C library function freopen performs the same operation after first closing any open stream associated with its parameter.

They are defined as

FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);

The fdopen function is not standard in C89 or C99, but is an extension used in POSIX environments and imitated elsewhere.

The mode parameter is a string that begins with one of the following sequences:

mode description starts..
r rb open for reading beginning
w wb open for writing (creates file if it doesn't exist). Deletes content and overwrites the file. beginning
a ab open for appending (creates file if it doesn't exist) end
r+ rb+ r+b open for reading and writing beginning
w+ wb+ w+b open for reading and writing. Deletes content and overwrites the file. beginning
a+ ab+ a+b open for reading and writing (append if file exists) end

The 'b' stands for binary. The C standard gives two kinds of files - text files and binary files - although operating systems may or may not distinguish between the two. A text file is a file consisting of text arranged in lines with some sort of distinguishing end-of-line character or sequence (in Unix, a bare linefeed character; in the Macintosh OS, a bare carriage return; on DOS and Microsoft Windows, a carriage return followed by a linefeed). When bytes are read in from a text file, an end-of-line sequence is usually mapped to a linefeed for ease in processing. When a text file is written to, a bare linefeed is mapped to the OS-specific end-of-line character sequence before writing. A binary file is a file where bytes are read in "raw," and delivered "raw," without any kind of mapping.

When a file is opened with update mode ( '+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, writes cannot be followed by reads without an intervening call to fflush() or to a file positioning function ( fseek(), fsetpos(), or rewind()), and reads cannot be followed by writes without an intervening call to a file positioning function. [1]

Writing and appending modes will create a file to write to in the case that the file name doesn't already exist. However, the operation of fopen is undefined if the filename doesn't follow requirements by the OS. For example, if the filename contains illegal characters , the program might crash. For example, in windows \ /: * ? > < and | cannot be part of a file name.

fclose

int fclose(FILE *file_pointer)

It takes one argument: a pointer to the FILE structure of the stream to close, eg: :fclose(my_file_pointer) This line call the function fclose to close FILE stream structure pointed by my_file_pointer.

The return value is an integer with the following meaning:

  • 0 (zero): the stream was closed successfully;
  • EOF: an error occurred;

One can check for an error by reading errno. fclose has undefined behavior if it attempts to close a file pointer that isn't currently assigned to a file - in many cases, this results in a program crash.

Example usage

The following program opens a file called myfile.txt, scans for an integer in it, then closes the file.

 #include <stdio.h>

 int main(void) 
 {
   FILE *file_pointer;
   int i;
 
   file_pointer = fopen("myfile.txt", "r");
   fscanf(file_pointer, "%d", &i);
   printf("The integer is %d\n", i);
   fclose(file_pointer);
   
   return 0;
 }

See also