Help Needed W/ Code..please!!

This is a discussion on Help Needed W/ Code..please!! within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to read from a file into a structure but my program crashes immediatedly..I reckon the problem ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Help Needed W/ Code..please!!

    Hi,
    I am trying to read from a file into a structure but my program crashes immediatedly..I reckon the problem lies w/ the fscanf statement but I ain't sure..please check..thanks
    A

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>


    /* Prototype Declarations */

    typedef struct{ char* names;
    int id;
    int scores[5];
    } STRUCT;


    int fillStructure(STRUCT table[]);

    int main(void)
    {


    STRUCT table[50];

    int check;




    check = fillStructure(table);

    return 0;
    }

    int fillStructure(STRUCT table[])
    {

    FILE *fpnum;
    STRUCT *pStu;
    STRUCT *pLastStu;

    pLastStu = table + 49;

    // Opens lab5.txt
    if (!(fpnum = fopen ("lab5.txt", "r")))
    for(pStu = table; pStu < pLastStu; pStu++)
    {
    fscanf(fpnum, "%s %d %d %d %d %d %d", table->names, table->id, table->scores[0], table->scores[1],
    table->scores[2], table->scores[3], table->scores[4]);
    }


    else
    {
    printf("\aError: Was Unable to Open File.\n");
    return 1;
    }
    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First off, you've done enough posts here to know how to use code tags by now....!

    Now:
    Code:
    typedef struct
    {
       char* names; 
       int id; 
       int scores[5]; 
    } STRUCT;
    The names variables is only a pointer. You'll need to assign it some memory if you want to store anything in it. Alternatively, (and the way I suggest), use an array: char names[NAME_LENGTH];

    Second, fscanf parameters are pointers, so you need to pass the address of table->scores[4]

    There may be more probs, I haven't looked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Additional to Hammer.

    >pLastStu = table + 49;

    This positions the pointer 49 places in the memory further, but you want it to be 49 structures in the memory further. In other words, you must also use the size of the struct in your calculation.

  4. #4
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    This positions the pointer 49 places in the memory further, but you want it to be 49 structures in the memory further. In other words, you must also use the size of the struct in your calculation.
    Both table and pLastStu are STRUCT*. It looks correct to me.

    First off, as Hammer said, make names an array of an appropriate size, something like 50. If you decide to keep your names declared as char* then you want to allocate some memory for it. Something like:

    Code:
    names = malloc (50);
    Second, you have coded a loop that handles the subscripts of scores members. But what about table members ?? You have that part of the code in the calling function?

    scanf arguments must be addresses. You must add an ampersand to all arguments eccept for names. names is already an address.

    Using scanf is never the best solution. The format string must exactly coincide with the text format in the file. I would suggest loading line by line with fgets and then parsing the line with string functions.
    Last edited by char; 05-28-2002 at 04:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed with simple code
    By Branka in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2005, 10:25 AM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. Help needed w/ line of code..Thanks!
    By aspand in forum C Programming
    Replies: 7
    Last Post: 05-30-2002, 04:12 PM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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