Data Structures and File Streams

NULL is defined in the stdio.h header file. NULL is defined as either 0 or (void*)0; either way, the two values evaluate to logical false and, when compared to each other, return true. Still, it’s not a good idea to initialize a pointer to the constant value 0.

#include <stdio.h>
#include <stdlib.h>

/* ------ main function ------- */

int main ( int argc, char *argv[] )
        printf("NULL is true.\n");
    } else {
        printf("Null is false.\n");

        printf("0 == NULL\n");
    } else {
        printf("0 != NULL\n");

    return EXIT_SUCCESS;

/* ----------  end of main function  ---------- */

 Remember, when using pointers, we ought to use NULL, and not 0.

When data is read as a character stream, the application requests each character sequentially, each of which is returned as an integer (not a char) until end-of-file is reached. When EOF is reached,  a negative value, which cannot represent an ASCII character, is returned.

The main concept behind stream I/O is the files can be seen as ordered sequences of characters, which can likewise be processed in a sequential manner.

The getchar() and putchar() functions are usually implemented as macros, which makes them even faster.

 Our next program will read a flat text file and use the information to populate an array of structures. The flat text file, named sales.txt, is as follows:

Atlanta 782038.46
Austin 299772.84
Chicago 816093.76
Dallas 751815.11
Denver 936772.82
Houston 759756.27
Indianapolis 574831.60
Kansas City 844308.93
Los Angeles 969770.37
Mexico City 510140.09
Minneapolis 787488.02
Montreal 316987.23
New York 657590.42
Pittsburgh 898253.56
St. Louis 677488.73
San Francisco 513126.19
Toronto 905377.23

Each line will correspond to a structure that contains two fields, the regional office name, a char array, and the amount of sales, a double value.

The following program will read through this list using the fgetc() function, which retrieves a single character from the stream at a time. We will also use the ungetc() function to return a character to the stream; we will do this when implementing a peek() function of our own design.

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

#define TRUE 1
#define FALSE 0
#define BUFFER_SIZE 256

struct sales
    char office[OFFICE_NAME_LENGTH+1];
    double revenue;

char buffer[BUFFER_SIZE];

char peek(FILE *fp);

/* ------ main function ------- */

int main ( int argc, char *argv[] )
    struct sales SalesInfo[20];
    double total = 0;
    int ch, ch2;
    int i = 0;
    int entry = 0;
    int word = 0;
    int endOfWord = FALSE;
    int endOfLine = FALSE;

    FILE *fp = NULL;

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

    //let's do an explicit test
        printf("Could not open file.\n");
        return EXIT_FAILURE;

        //item is finished when we
        //hit end of line
        //or space without a number after it
            endOfWord = TRUE;
            endOfLine = TRUE;
        //check to see if space is just part of
        //city name or else if it is between
        //the name and number
        if(ch==' '){
            ch2 = peek(fp);
            if('0' <= ch2 && ch2 <= '9'){
                endOfWord = TRUE;

            buffer[i++] = ch;
        } else {
            //put string termination character
            buffer[i] = '';
            if(word % 2 == 0){
                strcpy(SalesInfo[entry].office, buffer);
            } else {

                SalesInfo[entry].revenue = atof(buffer);
            i = 0;
            //reset end of word flag
            endOfWord = FALSE;
        //check for end of line
            //new line, new entry
            //reset flag
    }//end while loop

    for(i = 0; i < entry; i++){
        //format office field to be 32 chars in length
        //format double to display two digits to the right
        //of the decimal
        printf("%32s  $ %-32.2f\n", SalesInfo[i].office, SalesInfo[i].revenue);


    return EXIT_SUCCESS;

/* ----------  end of main function  ---------- */
char peek(FILE *fp){
    char c = fgetc(fp);
    ungetc(c, fp);
    return c;

When dealing with flat text files that store data, it’s best to have some sort of delimiter, that is to say a special character that separates the fields. Commas and colons are the two delimiters most often used. For our final program, we will use colons.

Kansas City:MO:394415.29:25890.03:Johnson
Los Angeles:CA:578920:99141.30:Verma
San Francisco:CA:751622.91:18520.31:Shen
St. Louis:MO:395114.47:68191.38:Foshay

We will name the above file sales2.txt. It consists of eleven lines, with each line being a record. Each line is divided into five fields. The first field contains the city, the second field contains the state, the third field contains the revenue, the fourth field contains the expenses, and the last field contains the last name of the manager.

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

int countRecords(FILE *pFile);

struct SRecord{
    char szCity[32];
    char szState[2];
    double dRevenue;
    double dExpenses;
    char szManager[32];

char buffer[256];

/* ------ main function ------- */

int main ( int argc, char *argv[] )
    int iCounter = 0;
    int iNumRecords;
    int iField = 0;
    int iRecord = 0;
    int iChar = 0;

    FILE *pFile = fopen("sales2.txt", "r");

        printf("Problem opening file.\n");

    iNumRecords = countRecords(pFile);

    printf("There are %d records.\n", iNumRecords);

    struct SRecord *pSRecords = malloc(sizeof(struct SRecord)*iNumRecords);

        if(iChar=='\n'|| iChar==':'){
                case 0:
                    strcpy(pSRecords[iRecord].szCity, buffer);
                case 1:
                    strcpy(pSRecords[iRecord].szState, buffer);
                case 2:
                    pSRecords[iRecord].dRevenue = atof(buffer);
                case 3:
                    pSRecords[iRecord].dExpenses = atof(buffer);
                case 4:
                    strcpy(pSRecords[iRecord].szManager, buffer);
            //move to next record
            //reset field count
                iField = 0;
            } else {
                //move to the next field
            //reset buffer counter
            iCounter = 0;
        } else {
    }//end while loop

    for(iRecord = 0; iRecord < iNumRecords; iRecord++){
        printf("%32s %3s",pSRecords[iRecord].szCity, pSRecords[iRecord].szState);
        printf("%12.2f \t %12.2f\n", pSRecords[iRecord].dRevenue, pSRecords[iRecord].dExpenses);

    //free the memory

    return EXIT_SUCCESS;

/* ----------  end of main function  ---------- */

//count the number of records
int countRecords(FILE *pFile){
    int iReturnVal = 0;
    int iTemp;
    //count number of newlines
    }//end while loop


    return iReturnVal;
// ---- end countRecords() -----

In the program above, we dynamically allocated the array of structs based on the number of lines we counted in the file. Note that we rewound the file pointer back to the beginning of the file after counting the number of lines.









Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s