Like Tree1Likes
  • 1 Post By laserlight

read data from file into 2 dimensional arrays help

This is a discussion on read data from file into 2 dimensional arrays help within the C Programming forums, part of the General Programming Boards category; Code: /*===============================getdata===================================== Pre: SpData is oppened, Post: */ int getData(FILE* spData, char name[][MAX_COL], float difficulty[], float score[][SCORE]) { int i ...

  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
    22,303
    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.
    quzah likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:56 PM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 10: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

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