File IO - Input Output

Advertisements

File IO in C

As we know, at the time of execution, every program comes in main memory to execute. Main memory is volatile and the data would be lost once the program is terminated. If we need the same data again, we have to store the data in a file on the disk. A file is sequential stream of bytes ending with an end-of-file marker.

Types of file supported by C:

  • Text Files
  • Binary Files

Difference between text file and binary file

  • Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable.
  • A newline(\n) character is converted into the carriage return-linefeed combination before being written to the disk. In binary file, these conversions will not take place.
  • In text file, a special character, whose ASCII value is 26, is inserted after the last character in the file to mark the end of file. There is no such special character present in the binary mode files to mark the end of file.
  • In text file, the text and characters are stored one character per byte. For example, the integer value 1245 will occupy 2 bytes in memory but it will occupy 5 bytes in text file. In binary file, the integer value 1245 will occupy 2 bytes in memory as well as in file.

Declaration of File Pointer

To access any file, we need to declare apointer to FILE structure and then associate it with the particular file. This pointer is referred to as file pointer.

Syntax for declaring file pointer


				              FILE * fp;
				

A pointer to FILE structure contains information, such as size, current file pointer position, type of file etc., to perform operation on the file.

Opening a file using fopen() function

The fopen() function takes two arguments, the name of the file amd the mode in which the file is to be opened. Mode specify the purpose of opening the file i.e, whether for reading or writing.

Syntax for opening the file in C


              fp = fopen(char *filename,char *mode);

When fopen() function opens a file in memory, it returns a pointer to this particular file. If fopen() function can't open the file then it will return NULL.

Example for opening the file in C


       void main()
       {
              FILE *fp;

              fp = fopen("file1.txt","r");      //Statement   1

              if(fp == NULL)
              {
                     printf("\nCan't open file or file doesn't exist.");
                     exit(0);
              }

In the above example, statement 1 will open an existing in text mode and return a pointer to file. If file will not open, an appropriate message will be displayed.

File Opening Modes

ModePurpose
"r" or "rt" Open a text file for reading, The file must already exist.
"w" or "wt" Open a text file for writing. If the file already exists, all the data will lost. If it doesnt exist, it will be created.
"a" or "at" Open a text file for appending. Data will be added at the end of the existing file. If file doesnt exist, it will be created.
rb Open a binary file for reading, The file must already exist.
wb Open a binary file for writing. If the file already exists, its contents will be overwritten. If it doesnt exist, it will be created.
ab Open a binary file for appending. Data will be added at the end of the existing file. If file doesnt exist, it will be created.
"rt+" or "r+t" Open text file for reading and writing. The file must already exist.
"wt+" or "w+t" Open text file for reading and writing. If file doesn't exist, it will be created.
"at+" or "a+t" Open text file for reading and appending. If file doesn't exist, it will be created.
"rb+" or "r+b" Open binary file for reading and writing. The file must already exist.
"wb+" or "w+b" Open binary file for reading and writing. If file doesn't exist, it will be created.
"ab+" or "a+b" Open binary file for reading and appending. If file doesn't exist, it will be created.

Closing a File using fclose() function

When the reading or writing of a file is finished, the file should be closed properly using fclose() function. The fclose() function does the followling tasks:

  • Flushes any unwritten data from memory.
  • Discards any unread buffered input.
  • Frees any automatically allocated buffer
  • Finally, close the file.

Syntax for closing the file in C


              int fclose( FILE* );

Example for closing the file in C


       void main()
       {
              FILE *fp;

              fp = fopen("file1.txt","r");

              if(fp == NULL)
              {
                     printf("\nCan't open file or file doesn't exist.");
                     exit(0);
              }

                    - - - - - - - - - -
                    - - - - - - - - - -

              fclose(fp);

       }

Reading and Writing File

We can read data from file and write data to file in many ways.

  • Reading or writing characters using fgetc() and fputc() functions. More info
  • Reading or writing string using fgets() and fputs() functions. More info
  • Reading or writing integers using getw() and putw() functions. More info
  • Reading or writing formatted IO using fscanf() and fprintf() functions. More info
  • Reading or writing records using fread() and fwrite() functions. More info

File Positioning Functions

File positioning functions are used to move the pointer in a file to the desired position, without closing and re-opning the file.

C provides followling three functions to move the pointer in the file:

  • The rewind() function is used to move the pointer to the beining of the file. More info
  • The ftell() function is used to retrieve the current position of pointer in the file. More info
  • The fseek() function is used to move the pointer to the desired position in the file. More info

Related topics

Advertisement