Thread: Storing words from a file to an array

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Storing words from a file to an array

    Hi I'm trying to store some words from an input file to an array, but I keep getting segmentation faults.
    The input text file looks something like this
    135 hello
    124 help
    1 first
    each word is separated by a tab after the number

    The following are some of my codes that are relevant:

    typedef struct mydata Mydata;
    struct mydata {
    int key[5000];
    char *name;
    };

    void inputfile() {
    FILE *input;
    int i, key;
    char name[100];
    Mydata array;

    input = fopen ("testdata.txt", "r");

    for(i=0;i<=MAX && !feof(input); i++)
    {
    fscanf (input, "%d%s", &key, &name);
    array.key[i] = key;
    array.name[i] = strdup (name);
    }

    now say, if I want to do
    printf ("%d%s", array.key[0], array.name[0]);
    I get segmentation fault

    the problem here I think it's I've used strdup wrong, or storing the words in wrong. As it can store numbers with no errors.

    Can someone suggest to me what I'm doing wrong? Have I not assigned spaces for array.name correctly?

    Thanks in advance,
    Will

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: Storing words from a file to an array

    Hi SIKCAR,

    The strdup looks fine. Only the array and indexing the array is not correct:
    Code:
    typedef struct mydata Mydata;
    
    struct mydata {
              int key; /* removed 5000 (I asume MAX = 5000?) */
              char *name;
    };
    
    void inputfile() {
            FILE *input;
            int i, key;
            char name[100];
            Mydata array[MAX];
    
            input = fopen ("testdata.txt", "r");
    
            for(i=0;i<=MAX && !feof(input); i++)
            {
                   fscanf (input, "%d%s", &key, &name);
                   array[i].key = key;
                   array[i].name = strdup (name);
            }
    
    printf ("%d%s", array[0].key, array[0].name);

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    B.t.w. always check return values:
    Code:
    if((input = fopen ("testdata.txt", "r")) == NULL)
    {
       /* fopen failed... */
    }
    
    if(fscanf (input, "%d%s", &key, &name) != 2)
    {
       /* failed to scan 2 items... */
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>for(i=0;i<=MAX && !feof(input); i++)
    Don't use feof() in this manner. There are plenty of posts on here telling you why this is wrong.

    And the struct would be better written like this:
    Code:
    typedef struct 
    {
        int key; 
        char *name;
    } Mydata;
    Also, check the return from fopen() to ensure it worked.

    Don't forget to free() all the data you malloc()'d (they're hidden within strdup() ).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    Thanks for that, I have also checked for the return values.

    But now I have error compiling the line
    array[i].name = strdup (name);

    "warning: assignment makes pointer from integer without a cast"

    I've tried changing
    char *name; to juse char name;
    in my struct
    but get segmentation fault that way.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    also, I'm not too familiar at using strdup
    can you please explain how to free the memory malloc()'d by this function?

    Thanks,
    Will

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>"warning: assignment makes pointer from integer without a cast"
    What libraries are you #include'ing ? Do you have string.h?

    Freeing the malloc'd data example:
    Code:
    char *name;
    name = strdup(SomeCharArray);
    /* strdup could return NULL, we should really be checking for that. */
    puts(name);
    free(name);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    Oh, I forgot to include string.h
    thanks for that

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Bug

    And just a bug in the program, with the very same code.
    I somehow take in the last element of my input file in twice, and printing them out twice in my for loop.
    ie it prints
    456 aaa
    124 abc
    124 abc
    where there was only one 124 abc in the input file.

    Can anyone see the problem?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Bug

    Originally posted by SIKCAR
    And just a bug in the program, with the very same code.
    I somehow take in the last element of my input file in twice, and printing them out twice in my for loop.
    ie it prints
    456 aaa
    124 abc
    124 abc
    where there was only one 124 abc in the input file.

    Can anyone see the problem?
    Yes, and I've already told you why. You're using feof() incorrectly. Check the return from the reading function to determine if you're reached the end of the input.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    alright thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  5. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM