File Read/write

This is a discussion on File Read/write within the C Programming forums, part of the General Programming Boards category; Hello, I need a little help reading input from a file and placing it into a structure. The file looks ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    File Read/write

    Hello, I need a little help reading input from a file and placing it into a structure.

    The file looks like this:

    Code:
    rect
    7 9
    +-------+
    |       |
    |       |
    |       |
    |       |
    |       |
    +-------+
    ex
    5 5
    *   *
     * *
      *
     * *
    *   *
    dollar
    1 1
    $
    this particular file contains 3 separate patches which belong in 3 different structures. rect, ex, and dollar belong in the name portion of the structures, the two numbers belong with the height and width portions, and the symbols belong in the two-dimensional array.

    The stucture I have created:

    Code:
    #define M 24
    #define N 79
    #define NAME 10 
    
    typedef struct {
            char name[NAME];
            int height;
            int width;
            char patch[M][N];   
    }patch;
    I have opened the file but am unsure of how to go about filling the structure with the information contained within the file. Any help will be appreciated...

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    15
    i guess my real question is, how can I read the second line

    Code:
    7 9
    in as two separate integers?

    I tried reading the line in as a char array and creating tokens, but when I cast them like so:

    Code:
    p->height = token;
    the output is not so good.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    When reading the input from the file, use fscanf, like this:

    Code:
    FILE *f;
    patch p;
    ....
    fscanf(f,"%d %d",p->height,p->width);

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    15
    Iwill try that out...thanks.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by IsmAvatar2 View Post
    When reading the input from the file, use fscanf, like this:

    Code:
    FILE *f;
    patch p;
    ....
    fscanf(f,"%d %d",p->height,p->width);
    Tried this out and it causes my program to cras when it hits this line.

    Partial snippet:

    Code:
    while(fgets(line, sizeof(line), patches) != NULL) {
             strcpy(p->name, line);
             //debug
             printf("%s", p->name);                                                //prints fine
             system("PAUSE");                                                        //good here
             
             fscanf(patches,"%d %d", p->height, p->width);
             system("PAUSE");                                                        //never reaches this
    any ideas?

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    15
    I got it going now.

    Code:
    fscanf(patches,"%d %d", p->height, p->width);
    should have had the & before the variables...8)

    Code:
    fscanf(patches,"%d %d", &p->height, &p->width);
    Thanks for the help....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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