Thread: read data from file into 2 dimensional arrays help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    read data from file into 2 dimensional arrays help

    Code:
    /*===============================getdata=====================================
    Pre: SpData is oppened,     
    Post:
    */
    
    int getData(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE])
    
    {
         int i = 0;
    	 int j = 0;
       
    	 while (i < MAX && fscanf(spData,"%c", &name[i][j])!=EOF)
    	 {
    		 for( j = 1; j < MAX_COL; j++)
    		 {
    			 fscanf(spData,"%c",&name[i][j]); 
    		 }
    
    
    		    fscanf(spData,"%f", difficulty[j]); 
    	 
    	     for( j = 0; j < SCORE; j++)
    			 fscanf(spData,"%f", score[i][j]);
    		 i++;
    		 j++;
    	 }	 
    
       return i;
    
    }

    KNIFE JACK 1.3 6.0 5.1 6.3 5.9 6.5
    WILLIAMSON FLIP A 1.4 3.5 4.1 4.7 7.2 3.8
    SOMMER TODD 1.2 8.0 9.1 8.1 9.3 9.0
    SWAN MIKE 1.1 4.3 2.1 9.9 6.2 7.0

    my file openned but doesnt print the data above out
    Last edited by khoavo123; 02-02-2012 at 02:28 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you use normal loops like a normal person?
    Code:
    for( i = 0; i <MAX; i++ )
        for( j = 0; j < MAX_COL; j++ )
            if( fscanf( foo, "%f", &this[ i ][ j ] ) != 1 )
                ...some kind of error...
    Also, your code doesn't print anything, so why would you expect to see some output there?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your attempt to use fscanf to read the names looks wrong. One problem is that the names can contain spaces, yet a space is used as the field delimiter. Furthermore, they can consist of two or three (or more? or just one?) "words".

    Is the file format fixed? If not, you may wish to change it to make parsing easier.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    i guess its wrong cause it only print out the first line of the input data

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    why are you so aggressive?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-16-2010, 10:51 AM
  2. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. how do i read data from a file?
    By ssjnamek in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2002, 04:58 PM
  5. read data out of *.txt file
    By dune911 in forum C Programming
    Replies: 3
    Last Post: 12-14-2001, 12:33 PM