Thread: fill in a struct with fscanf

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    fill in a struct with fscanf

    Please I need help, I can't find my bug...

    data.txt contains:
    /* name surname integer integer */
    name surname 011 111
    name surname 1 2
    ...

    then:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct person {
        char *name, *surname;
        int age, income;
    };
    
    struct person *init(void)
    {
        struct person *p;
        p = (struct person *) malloc(sizeof(struct person));
        p->name = NULL;
        p->surname = NULL;
        p->age = 0;
        p->income = 0;
        return p;
    }
    
    int main(void)
    {
        FILE *fp = fopen("data.txt", "r");
        struct person *list[10]; /* no matter... */
        int register i = 0;
    
        while (!feof(fp)) {
            list[i] = init();
            fscanf(fp, "%s %s %d %d\n", list[i]->name, list[i]->surname, &(list[i]->age), &(list[i]->income));
            i++;
        }
        fprintf("%s %s\n", list[0]->name, list[0]->surname);
        fclose(fp);
        return 0;
    }
    But it does not work! can anyone provide help?
    Cheers
    DBG
    Last edited by GDB; 06-24-2012 at 10:44 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,498
    How does it not work?
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's not enough to just malloc sizeof(struct person) since you are reading strings to p->name and so on. p->name is just a pointer, it's meant to point to where your string are only, not contain the string.

  4. #4
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,758
    AS a side note
    Code:
    while (!feof(fp))
    do not use feof to control input loop - read FAQ FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    To be or not to be == true

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    4
    Quote Originally Posted by laserlight View Post
    How does it not work?
    Actually, line 32, we should have in output: name surname 011 111

    but it does not...

    fprintf(stdout, "%s %s\n", list[1]->name, list[1]->surname);

    should printout regarding data.txt:

    name surname 1 2

    Instead I do have: core dumped...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,684
    Start with
    Code:
    struct person {
    //    char *name, *surname;
        char name[100], surname[100];
        int age, income;
    };
    until you understand the finer points of memory allocation.

    And please read the FAQ on how to use feof() properly (which you are not).
    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
    Jun 2012
    Posts
    4
    Quote Originally Posted by Salem View Post
    Start with
    Code:
    struct person {
    //    char *name, *surname;
        char name[100], surname[100];
        int age, income;
    };
    until you understand the finer points of memory allocation.
    Thanks. I went for p->name = malloc(/* */);

    And please read the FAQ on how to use feof() properly (which you are not).

    Yes SIR!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error on fscanf when reading a struct from a file
    By faustian_1 in forum C Programming
    Replies: 3
    Last Post: 10-23-2011, 01:18 PM
  2. Fill in a struct from file
    By MTK in forum C Programming
    Replies: 4
    Last Post: 06-07-2010, 04:32 PM
  3. segfault in fscanf and struct question
    By AstralZecha in forum C Programming
    Replies: 15
    Last Post: 03-28-2010, 09:58 AM
  4. Replies: 13
    Last Post: 11-18-2009, 01:05 AM
  5. Quick Fill "struct"s
    By Epo in forum C++ Programming
    Replies: 10
    Last Post: 12-26-2003, 05:52 PM

Tags for this Thread

Website Security Test