Thread: Text File to Struct

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    Text File to Struct

    Hi,
    Need some help reading a text file to struct

    .txt file attached.

    so what i need to do is read the name, the ascii picture, and rating to a struct.

    Code:
    typedef struct {
        char name[50];
        char picture[100];
        int  rate;
    }pic
    ;

    tried fgets with sscanf, but that reads each line of the file, which would be fine for the name but i would like to place the whole ascii picture to one array.

    some guidance would be appreciated.
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So you have this repeated
    Code:
    Jean Pajerek
      |\_____/|
      |[o] [o]|
      |   V   |
      |       |
     -ooo---ooo-
    # 5
    fgets() one line and store it in name.

    fgets() lines in a loop.
    If it begins with anything other than a #, store it in picture (you'll need a 2d array)
    if it begins with a #, parse out the number and store it in rate

    Simples - no?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    ok will try that out.

    so this can be accomplished with just fgets, sscanf, and a 2d array in the struct?

    thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > so this can be accomplished with just fgets, sscanf, and a 2d array in the struct?
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    im having some trouble getting the next line into second array in struct

    Code:
    while (fgets(line, sizeof(line), pFile))
        {
            sscanf(line, "%[^\n]", art->name);
        }
    it will keep overwriting the same array which i understand.

    just don't know how to read next following lines up until # to next array in struct.

    really noob at the algorithm.

    thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need some kind of state to tell you what you've done / or need to do next.
    Code:
    enum { DO_NAME, DO_PIC } state = DO_NAME;
    while (fgets(line, sizeof(line), pFile)) {
        if ( state == DO_NAME ) {
            sscanf(line, "%[^\n]", art->name);
            state = DO_PIC;
        } else if ( state == DO_PIC ) {
            if ( line[0] == '#' ) {
                // it's the rate
                state = DO_NAME;  // start with the next picture
            } else {
                // it's a pic line
            }
        }
    }
    Finite-state machine - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Thanks Salem,

    now just having trouble getting the picture filled in the 2d array

    Code:
    	while (fgets(line, sizeof(line), pFile) && i < 20){
    		if(state == DO_NAME){
    			sscanf(line, "%[^\n]", art[i].name);
    			state = DO_PIC;
    		} else if(state == DO_PIC){
    			if(line[0] == '#' ){
    				sscanf(line, "%s %d", buff, &art[i].rate);
    				state = DO_NAME;
    				i++;
    			} else {
    				for(r = 0; r < 20; r++){
    					for (c = 0; c <80; c++){
    						sscanf(line, "%s", art[i].picture[r][c]);	
    					}
    				}
    			}
    		}
    	}
    Thanks again for your help

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
                for(r = 0; r < 20; r++){
                    for (c = 0; c <80; c++){
                        sscanf(line, "%s", art[i].picture[r][c]);  
                    }
                }
    This is all wrong.
    You have a single line, so copy ONE line to ONE row of your picture data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    i think i got it,

    Code:
    	while (fgets(line, sizeof(line), pFile) && i < 20 && r < 20){
    		if(state == DO_NAME){
    			sscanf(line, "%[^\n]", art[i].name);
    			state = DO_PIC;
    		} else if(state == DO_PIC){
    			if(line[0] == '#' ){
    				sscanf(line, "%s %d", buff, &art[i].rate);
    				state = DO_NAME;
    				i++;
    				r = 0;
    			} else {
    				sscanf(line, "%[^\n]", &art[i].picture[r]);
    				r++;
    				}
    			}
    		}

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Hey salem,
    could you help me sort out the names by most frequent using selection sort?

    having trouble starting the sorting process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a text file into a C struct
    By someone2088 in forum C Programming
    Replies: 11
    Last Post: 12-07-2011, 10:14 AM
  2. read contents of a text file into a struct
    By lemonwaffles in forum C++ Programming
    Replies: 2
    Last Post: 08-03-2009, 02:20 PM
  3. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 11:37 AM
  4. Problems loading a text file into a struct of vectors
    By alt234 in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 09:58 PM
  5. Loading text from file into struct
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 05:33 PM