C Programming Files Handling


In this article you will learn about File Handling in C programming.

1. File Pointers

There are many ways to use files in C. The most straight forward use of files is via a file pointer.

FILE *fp; 

fp is a pointer to a file.

The type FILE, is not a basic type, instead it is defined in the header file stdio.h, this file must be included in your program.

2. Opening a File

fp = fopen(filename, mode);

The filename and mode are both strings.

The mode can be

  • r – read
  • w – write, overwrite file if it ex ists
  • a – write, but append instead of overwrite
  • r+ – read & write, do not destroy file if it exists
  • w+ – read & write, but overwrite file if it exists
  • a+ – read & write, but append instead of overwrite
  • b – may be appended to any of the above to force the file to be opened in binary mode rather than text mode
  • fp = fopen("data.dat","a"); – will open the disk file data.dat for writing, and any information written will be appended to the file.

The following useful table from the ANSI C Rationale lists the different actions and requirements of the different modes for opening a file:

file handling in C programming
file handling in C programming

fopen returns NULL if the file could not be opened in the mode requested. The returned value should be checked before any attempt is made to access the file. The following code shows how the value returned by fopen might be checked. When the file cannot be opened a suitable error message is printed and the program halted. In most situations this would be inappropriate, instead the user should be given the chance of re-entering the file name.

#include <stdio.h> int main() { char filename[80]; FILE *fp; printf("File to be opened? "); scanf("%79s", filename); fp = fopen(filename,"r"); if (fp == NULL) { fpri ntf(stderr, "Unable to open file %s\n", filename); return 1; /* Exit to operating system */ } //code that accesses the contents of the file return 0; }
Code language: C/AL (cal)

Sequential file access is performed with the following library functions.

  • fprintf(fp, formatstring , ...) – print to a file
  • fscanf(fp, formatstring , ...) – read from a file
  • getc(fp) – get a character from a file
  • putc(c, fp) – put a character in a file
  • ungetc(c, fp) – put a character back onto a file (only one character is guaranteed to be able to be pushed back)
  • fopen( filename , mode) – open a file
  • fclose(fp) – close a file

The standard header file stdio.h defines three file pointer constants, stdin ,stdout and stderr for the standard input, output and error streams. It is considered good practice to write error messages to the standard error stream.

Use the fprintf() function to do this:

fprintf(stderr,"ERROR: unable to open file %s\n", filename);

The functions fscanf() and getc() are used for sequential access to the file, that is subsequent reads will read the data following the data just read, and eventually you will reach the end of the file. If you want to move forwards and backwards through the file, or jump to a specific offset from the beginning, end or current location use the fseek() function (though the file mustbe opened in binary mode). The function ftell() reports the current offset from the beginning of the file.

If you wish to mix reading and writing to the same file, each switch from reading to writing (or vice versa) mustbe preceded by a call to fseek()fsetpos()rewind() or fflush(). If it is required to stay at the same position in the file then use fflush() or fseek(fp,0L,SEEK_CUR) which will move 0 bytes from the current position!

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks