1

Introduction to C File-based Input and Output

The functions which are used for read and write data to a file are specified in stdio.h. When performing file operations, the three special files are used (stdin, stdout, and stderr):

  • stdin is the standard input file. It’s used for reading inputted data from keyboard.
  • stdout is the standard output file. It’s used for writing data onto the screen.
  • stderr is the standard error file. It’s used for writing output errors onto the screen.

The basic steps for reading/writing the data from/to a file are follows:

  1. Open the file
  2. Read/write data from/to the file
  3. Close the file

Two types of files can be opened in C – text files and binary files:

  • Text files only could contain printable characters and can be read and written on a line-by-line bases
  • Binary files could contain any data and can be read and written in fixed blocks of data

During the opening process of the file, one of these two file types needs to be specified in order to open the file.

These are the common functions used to do the file operations:

  • fopen and fclose
  • fprintf and fscanf (for text files)
  • fputs and fgets (also for text files)
  • fputc and fgetc (also for text files)
  • fread and fwrite (for binary files)
  • fseek (also for binary files)

Note that some of the above functions are different from the following corresponding functions:

  • open and close
  • read and write
  • seek
0

More C Strings Input and Output Functions

For the input function, the gets function is much more simpler to use than the scanf function. The scanf function is often used in the advanced C programs. The same is for the output function. the puts function is more simpler to use than the printf function. Here are the definitions for these functions:

  • puts – Put String
  • gets – Get String
  • fgets – File Get String

As you can see in the above definitions, the fgets function is used to get the value from a file. However, fgets is also often used in place of the gets function because gets is mostly misused by programmers.

Here is the sample code for these functions:

/*
 *
 * string.c
 *
 * Program to illustrate the use of gets, puts and fgets
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>
#include <string.h>

main() {
 char name[21];

 puts("This is a basic programn");

 fputs("Please enter your full name (20 characters max): ", stdout);
 fgets(name, 21, stdin);

 fputs("Your full name is ", stdout);
 puts(name);

 fflush(stdin);
 if (strlen(name) >= 20)
 while (getchar() != 'n' && getchar() != EOF) {}
 getchar();
}

Note that when I used GCC to compile the above code with gets function, the GCC compiler would give me a warning stated that the gets function is dangerous and should not use it. Therefore I recommend to use fgets to make things easier.

Also notice that the above code contains a new function, fputs. As my current knowledge, the difference between fputs and puts is that puts automatically adds a break (n) at the end of each output, whereas fputs does not. The fputs function is good to display the prompt for user to enter the data.