Simple File IO in C


Non-File Input in C
The C equivilant to the C++ function cin is "scanf". The first argument to scanf tells the system what type of information to read.
%s - string
%d - decimal number
%f - floating point number
%c - single character
The remaining arguments to scanf are the memory locations to put the input.

So, the following reads two integers:
    int A, B;
    scanf("%d %d", &A, &B);

Notice that scanf needs the location of where to put the input, not the names of the variables.

Since arrary names are really just the starting location of a list of things, the & is not needed for strings:
    char name[20];
    scanf("%s", name);


Non-File Output in C
The C equivilant to the C++ function cout is "printf". Printf is very similiar to scanf. Printf prints a single string, which can be composed of the contents of lots of variables.

The following example prints the contents of two integer variables:
    int A, B;
    printf("A = %d and B = %d \n", A, B);

Note there is no "endl" in C, so use "\n".

Here is an example with strings:
    char name[20];
    printf("The first letter of the string %s is %c\n", name, name[0]);


File IO in C

The functions fprintf and fscanf print to and read from files, instead of standard input and output. The first argument to each is a pointer to a file.

The function "fopen" opens a file for either input or output (actually there are more choices). "r" means read and "w" means write. If the open was not successful, then the pointer will be set to NULL. The data type "FILE" is provided in <stdio.h>.

Every linux program, written in C or C++ or anything else, always has three open files - standard input, standard output, and standard error. Your C program can use fprintf or fscanf to access those three files. The pre-set names are stdin, stdout, and stderr. Notice that the following program uses stderr as the location to send error messages.

Here is a tiny program to copy the file named file1 into file2:

/**********************************************
Demo Three - Use C to copy file1 to file2
*********************************************/
#include <stdlib.h>
#include <stdio.h>

/******************************************************/

int main ()
{
   FILE *infile, *outfile;
   char string[20];

   /***** open input and output files *****/
   infile = fopen ("file1", "r");
   if (infile == NULL)
     {
      fprintf(stderr, "Error opening input file\n\n");
      exit (1);
     }
   outfile = fopen ("file2", "w");
   if (outfile == NULL)
     {
      fprintf(stderr, "Error opening output file\n\n");
      exit (1);
     }

   /***** copy everything  ****/
   while (!feof(infile))
     {
      fscanf  (infile,  "%s", string);
      fprintf (outfile, "%s ", string);
     }
}
Here is the program compiled and run:
> cc demo3.c

> cat file1
hi mom!

13 is a prime number.


> a.out

> cat file2
hi mom! 13 is a prime number. number.
>
The command "cc" compiles the program and creates the executable a.out.

Notice that everything is read as a string, even the number. The end-of-lines are lost because they are considered white-space, not part of a string to be read in.

Also notice that the last string "number." was repeated. That is because feof said we were not at the end of the file (because there was still a blank line left) but there were no more strings to read. A better approach would be to NOT use feof. Instead, read until fscanf no longer successfully reads input. Fscanf always returns -1 when it is unable to really read anything.