Files and Streams in C programming



Files and Streams

C views each file simply as a sequential stream of bytes. Each file ends either with an end-of-file marker or at a specific byte number recorded in a system-maintained, administrative data structure.

When a file is opened, a stream is associated with it.

Sequential-Access File in C
Sequential-Access File

Streams provide communication channels between files and programs. Three files and their associated streams are automatically opened when program execution begins:

  • the standard input to read data from the keyboard
  • the standard output to print data on the screen.
  • the standard error

Creating a Sequential-Access File

This program assumes the user enters the records in account-number order. The records would be sorted and written to the file.

   
    // Creating a sequential file
    #include <stdio.h>
    int main( void ) { 
    unsigned int account; // account number
    char name[ 30 ]; // account name
    double balance; // account balance
    FILE *cfPtr; // cfPtr = clients.dat file pointer   
    // fopen opens file. Exit program if unable to create file 
    if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
    puts( "File could not be opened" );
    } // end if
    else { 
      puts( "Enter the account, name, and balance." );
      puts( "Enter EOF to end input." );
      printf( "%s", "? " );
      scanf( "%d%29s%lf", &account, name, &balance );
    // write account, name and balance into file with fprintf
    while ( !feof( stdin ) ) { 
       fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
       printf( "%s", "? " );
       scanf( "%d%29s%lf", &account, name, &balance );
       } // end while
      
       fclose( cfPtr ); // fclose closes file   
       } // end else
    } // end main

    Output:
    Enter the account, name, and balance.
    Enter EOF to end input.
     100 Jones 24.98
     200 Doe 345.67
     300 White 0.00
     400 Stone -42.16
     500 Rich 224.62
     ^Z

Reading Data from a Sequential-Access File

Function fscanf receives a file pointer for the file being read. When the program reaches the end of the file, the file is closed and the program terminates.

Function feof returns true only after the program attempts to read the nonexistent data following the last line.

   
    // Reading and printing a sequential file
    #include <stdio.h>
    int main( void ) { 
    unsigned int account; // account number
    char name[ 30 ]; // account name
    double balance; // account balance
    FILE *cfPtr; // cfPtr = clients.dat file pointer
    // fopen opens file; exits program if file cannot be opened 
    if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
           puts( "File could not be opened" );
    } // end if
    else { 
    // read account, name and balance from file
      printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" );
      fscanf( cfPtr, "%d%29s%lf", &account, name, &balance );
    // while not end of file
    while ( !feof( cfPtr ) ) { 
      printf( "%-10d%-13s%7.2f\n", account, name, balance );
      fscanf( cfPtr, "%d%29s%lf", &account, name, &balance );
      } // end while
      fclose( cfPtr ); // fclose closes the file   
      } // end else
    } // end main

    Program output:
    Account     Name    Balance
    100         Samuel  24.98
    200         Martin  345.67
    300         White   0.00
    400         Stone   -42.16
    500         Rich    224.62

PROMOTIONS