Thread: Reading a mixed Text and decimal file into an array

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    HELP...Reading a mixed Text and decimal file into an array

    I am trying to read the following text file into an array containing the text data and an array containing the decimal data:

    First 0.5
    Second 0.39844
    Third 0.39844
    Fourth 0.5
    Second_TCC_Locked 0.39844
    Third_TCC_Locked 0.39844
    Fourth_TCC_Locked 0.5
    SS_Reverse 0.625

    My intent is to do a strcmp() later in the code in order to compare user input to this file.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	FILE *fpin0, *fpout;
        int c, i;
    	int count=0;
    	float table_gain1[8];
    	char table_gain0[8];
    	fpin0 = fopen("TextArray.txt","r");
        /* open file 'TextArray.txt' for reading */
    	while((c=fgetc(fpin0))!=EOF)
    	{
    		if(c=='\n')
    			count++;
    	}
    	/* do something with count */
    	printf ( "There are %d lines in the file\n",count );
    	
    	
    	/* Set pointer to beginning of file: */
    	fseek( fpin0, 0L, SEEK_SET );
    	
    	for(i = 0; i < count; i++)
    	{
    		fscanf(fpin0,"%s %d",&table_gain0[i], &table_gain1[i]);
    	}	
    	fclose(fpin0);
    	
    	fpout = fopen("TestRead.out","w");
    	fprintf(fpout, "Mode	Gain\n");
    	for(i = 0; i < count; i++)
    	{
    		
    		fprintf(fpout,"%s\n",&table_gain0[i]);
    	}
    	for(i = 0; i < count; i++)
    	{
    		
    		fprintf(fpout,"%d\n",&table_gain1[i]);
    	}
    	fclose(fpout);
    	
    }

    From the looks of the output, I am guess it is a format issue but I have been trying different formats without any luck. Any help would be greatly appreciated

    Output:

    Mode Gain
    F.S.T.F.5
    .S.T.F.5
    S.T.F.5
    .T.F.5
    T.F.5
    .F.5
    F.5
    .5
    1245004
    1245008
    1245012
    1245016
    1245020
    1245024
    1245028
    1245032
    Last edited by djamie; 08-04-2003 at 03:05 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    First of all...
    Code:
        /* open file 'TextArray.txt' for reading */
    	while((c=fgetc(fpin0))!=EOF)
    	{
    		if(c=='\n')
    			count++;
    	}
    Here after you open the file (which you donot check to see if it opened ok), you read each character from the file and increment the count. You are not counting each character, you are counting the number of lines there are. Your file contains one string and one double value per line.

    /* close file, do something with count */
    printf ( "There are %d lines in the file\n",count );

    Here you donot close the file at all (not in the code you posted).
    count is not the number of inputs read but the number of characters as mentioned previously. You then go on reading the file which is now at the end of the file. Therefore your next two loops will always evaluate false on the first iteration.

    Here is how i would preceed with this program.

    Variables
    one array of an array of characters
    one double array

    open file for reading and check if open correctly

    read each char array and one double into the required elements

    open output file for writing and check if open correctly

    write each char array and double to output file

    close files.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char table_gain0[8];
    You need a two dimensional array to store several strings:
    char table_gain0[8][20];

    >fscanf(fpin0,"%s %d",&table_gain0[i], &table_gain1[i]);
    Use %f for floats:
    fscanf(fpin0,"%s %f",&table_gain0[i], &table_gain1[i]);

    >fprintf(fpout,"%d\n",&table_gain1[i]);
    Again %f for floats, and no adress of (&) when using fprintf():
    fprintf(fpout,"%f\n",table_gain1[i]);

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    Originally posted by swoopy
    >char table_gain0[8];
    You need a two dimensional array to store several strings:
    char table_gain0[8][20];

    >fscanf(fpin0,"%s %d",&table_gain0[i], &table_gain1[i]);
    Use %f for floats:
    fscanf(fpin0,"%s %f",&table_gain0[i], &table_gain1[i]);

    >fprintf(fpout,"%d\n",&table_gain1[i]);
    Again %f for floats, and no adress of (&) when using fprintf():
    fprintf(fpout,"%f\n",table_gain1[i]);
    Thanks Swoopy!!!!!!! Did exactly what I wanted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 03:50 PM
  3. Reading in text file into an array, white spaces...
    By error in forum C++ Programming
    Replies: 12
    Last Post: 01-14-2003, 09:39 AM