Like Tree3Likes
  • 1 Post By Subsonics
  • 1 Post By vart
  • 1 Post By Salem

fill in a struct with fscanf

This is a discussion on fill in a struct with fscanf within the C Programming forums, part of the General Programming Boards category; Please I need help, I can't find my bug... data.txt contains: /* name surname integer integer */ name surname 011 ...

  1. #1
    GDB
    GDB is offline
    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 09:44 AM.

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

  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.
    Salem likes this.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    Salem likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    GDB
    GDB is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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).
    Subsonics likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    GDB
    GDB is offline
    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, 12:18 PM
  2. Fill in a struct from file
    By MTK in forum C Programming
    Replies: 4
    Last Post: 06-07-2010, 03:32 PM
  3. segfault in fscanf and struct question
    By AstralZecha in forum C Programming
    Replies: 15
    Last Post: 03-28-2010, 08:58 AM
  4. Replies: 13
    Last Post: 11-18-2009, 12:05 AM
  5. Quick Fill "struct"s
    By Epo in forum C++ Programming
    Replies: 10
    Last Post: 12-26-2003, 04:52 PM

Tags for this Thread


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