reading from file to an array - help needed!

This is a discussion on reading from file to an array - help needed! within the C Programming forums, part of the General Programming Boards category; basically its part of a bigger program but its this bit that doesn't work. its supposed to read the numbers ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    reading from file to an array - help needed!

    basically its part of a bigger program but its this bit that doesn't work.
    its supposed to read the numbers from a txt file (datafile) and write them to an array (fromfile)
    the datafile looks like this but it can be up to 40 lines
    105683,23,65,0,100
    105475,96,72,35,61
    105286,23,45,15,86
    104762,23,17,5,56

    this is what i have so far.
    Code:
    int main (void)             
    {
        FILE *datafile, *dataout;
        int fromfile[200]={0};
        int a=0,count = 0, b = 0,e=0 ,s=0, count2 =0;
    
      if ((datafile = fopen ("datafile.txt","rt")) == NULL)
    	 {
    		printf("\nError opening input file, program exiting\n");
                    return(1);
              }
    
    if ((dataout = fopen ("dataout.txt","wt")) == NULL)
       {
       printf("error opening output file, program exiting\n");
       return(1);
       }
    printf("\nProgram reading data from file\n");
    do    {fscanf(datafile, "%d", &fromfile[a]);
           printf(".");
           printf("%d",fromfile[a]); //i added this to see what is happening, it won't be in the final program            
           a += 1;
           count += 1;
           }while (fromfile[a]!=EOF);
    
    stdnts = count/5;
    at the moment does the first number correctly, but from then on it just displays zeros till it gets to the end of the array.(see attached image)

    thanks in advance, this is driving me mad.
    Attached Images Attached Images  

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    One of your counter/indexing variables is redundant, you don't need both a and count. I'd suggest maybe something like this for the loop:

    Code:
    int count = 0;
    
    ...
    
    while( (count < 200) && (fscanf(datafile,"%d",fromfile+count) == 1) )
    {
        if( ++count % 5 ) fgetc(datafile); // Skip the comma
    }
    After the above, the variable count will contain the number of values read/stored in the array.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You're overlooking the thing(s) separating the numbers -- a comma or a newline. A quick patch:
    Code:
       while ( fscanf(datafile, "%d%*c", &fromfile[a])  == 1 )
       {
          printf(".");
          printf("%d",fromfile[a]); //i added this to see what is happening, it won't be in the final program
          a += 1;
          count += 1;
       }
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop
    Last edited by Dave_Sinkula; 04-28-2005 at 01:18 PM. Reason: D'oh! Old and slow.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 11:37 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 04:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21