faqts : Computers : Programming : Languages : C

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 18 people (50%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How do i read a text file in c and then prepare an indexer of the words i have read

Oct 3rd, 2003 01:12
nial mueck, Pinak Desai,


This functon is from a program the i made some time a go, anyway what
this dose is it loads the information into a pointer array

int 
loadTowns( NodePTR *topPTR, char *argv[] )
    {
    /* Declarations */  
    char townTMP[22];
    char distTMP[8];
    char totalTMP[8];
    char stateTMP[4];
    int max;

    NodePTR newPTR;
    FILE *IFilePTR;

    /* Initialisation */
    max = 0;

    if (( IFilePTR = fopen( argv[1], "r" )) != NULL ) /* opens the input
file for reading */

    fscanf( IFilePTR, "%s %s %s %s", townTMP, distTMP, totalTMP,
stateTMP ); /* scans the file */
    while ( !feof ( IFilePTR )) /* irritates while it is not the end of
file */
        {

        newPTR = malloc( sizeof( TownNode )); /* allocated sufficient
space in memory for each entry */
 
        if ( newPTR != NULL ) /* if there is space in mem */
            {
            /* copys the scaned data to the pointer location */
            strcpy ( newPTR->town, townTMP );
            strcpy ( newPTR->dist, distTMP );
            strcpy ( newPTR->total, totalTMP );
            strcpy ( newPTR->state, stateTMP );
        
            newPTR->nextNodePTR = *topPTR; /* next pointer */
            *topPTR = newPTR; /* assigns newPTR to a blank pointer */
   
            fscanf( IFilePTR, "%s %s %s %s", townTMP, distTMP, totalTMP,
stateTMP );   
            max++; /* adds on to the town counter */
            }
         else
            {
            printf( "\nNo mem available\n" ); 
            }      
         }
    fclose ( IFilePTR );
    printf ( "\nTowns loaded.\n\n" );
    pause(); 
    return max; /* returns the maximum towns in the list */   
    }

this function will read the pointer list and print to the stdio.

void 
printTo( NodePTR currPTR, char *argv[], int IorO, int from, int to )
    {
    /* Declarations */
    char townTMP[22];
    char distTMP[8];
    char totalTMP[8];
    char stateTMP[4];
    int counter;
  
    FILE *FilePTR;

    /* Initialisation */
    counter = 0;
                
    if ( currPTR == NULL )
        {
        printf( "the file is empty! \n" );
	}
    else
        {
         if ( IorO == 1 && ( FilePTR = fopen( argv[IorO], "w" )) != NULL )
             {
             /* this tests to see if it is input file and if so opens it
for rewrites*/
             }   
         else if ( IorO == 2 && ( FilePTR = fopen( argv[IorO], "a+" ))
!= NULL )
            {
            /* where as this test to see if it is the output file and if
so opens it for appending & reading */
            }
	fprintf( FilePTR, "Towns added from here\n\n" ); /* to make clear where
the last group of towns was added */
        while ( currPTR != NULL )
            {
            /* copies the pointer value to the temp value */
            strcpy ( townTMP, currPTR->town );
            strcpy ( distTMP, currPTR->dist );
            strcpy ( totalTMP, currPTR->total );
            strcpy ( stateTMP, currPTR->state );
    
            if ( from <= counter && to >= counter ) /* allows the
parameter of what is to be displayed to be changed */
                {                                    /* adds the data to
the file */
                fprintf ( FilePTR, "%-22s%-8s%-8s%-3s\n", townTMP,
distTMP, totalTMP, stateTMP );
                }
            currPTR = currPTR->nextNodePTR; /* increments the pointer */ 
           counter++;                 
           }

        fclose ( FilePTR ); /* closes the file */
        }    
    }

lastly this is the pointer structure.

struct townNode
    {
    char town[22];
    char dist[8];
    char total[8];
    char state[4];
    
    struct townNode *nextNodePTR;
    };

typedef struct townNode TownNode;
typedef TownNode *NodePTR;