Question about reading strings from file

This is a discussion on Question about reading strings from file within the C Programming forums, part of the General Programming Boards category; It appears that when you enter command line arguments or use fgets() to input a string you can assign that ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Question about reading strings from file

    It appears that when you enter command line arguments or use fgets() to input a string you can assign that string to another string variable using the assignment operator. But when you read from a file, you can't do that, you get a segfault. It seems the only way to get around that is to malloc the string and use the strcpy function. Can anyone explain?

    Code:
    #include <stdio.h>
    
    
    struct person {
        char *names[2];
    };
    
    
    void readFile(struct person p){
        FILE *file = fopen("names", "r");
        int idx = 0;
        char line[20];
        while(fgets(line, 20, file)){
            p.names[idx++] = line;
        }
        fclose(file);
    }
    
    
    int main(int argc, char *argv[]){
        struct person p;
        //p.names[0] = argv[1];
        //p.names[1] = argv[2];
        readFile(p);
        printf("This is what you typed: %s %s\n", p.names[0], p.names[1]);
    
    
    }
    Last edited by atac; 11-02-2013 at 01:23 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    when you assign pointer to point to local buffer - this is what you do - make 2 pointers point to local buffer - which will be not there after you exit function.

    AS you correctly deducted - one way would be allocate memory for string and copy string with strcpy

    Another way - since you suppose that input is less than 20 characters (based on your line buffer) - you can declare names member not as array of 2 pointers to char but as array of 2 arrays of chars each 20 bytes long

    Code:
    struct person {
        char names[2][20];
    };
    Then you will not need to allocate memory with malloc - but still to copy C-strings you will have to use strcpy

    In C you cannot assign strings using assignment operator no matter how you have read the string.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by atac
    It appears that when you enter command line arguments or use fgets() to input a string you can assign that string to another string variable using the assignment operator. But when you read from a file, you can't do that. It seems the only way to get around that is to malloc the string and use the strcpy function. Can anyone explain?
    Strictly speaking, there is no such thing as a "string variable" in standard C (as in among the types provided by the language and the standard library). The notion of a string in standard C is really just "a contiguous sequence of characters terminated by and including the first null character".

    Therefore, when you say that "you can assign that string to another string variable using the assignment operator", what you're really talking about is pointer assignment: you have a pointer p1 to the first character of one string, and you assign that to another pointer p2. Hence, p2 points to the first character of the same string. Colloquially, we may say that p1 and p2 are strings rather than pointers, but strictly speaking they are pointers, hence there is no string assignment.

    So, when you read from a file, you read characters into an array, then null terminate that part of the array to get a string. If you want another array to contain a copy of that string, then you cannot use assignment since arrays cannot be assigned, hence using strcpy is one option. If have a pointer and want it to point to the first element of a copy of that string, then likewise you cannot use assignment: your pointer will be pointing to the first element of the same string, not a copy thereof. In that case, using malloc and then copying over with strcpy (or otherwise) is an option.
    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

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Code:
    struct person {
        char *names[2];
    };
    names is an array of 2 pointers to char. An pointer to char can point to an array of chars, which could be allocated through malloc() or just on the stack space. It is not an Array of char that can store a string, you have to allocate space to store the string then POINT the pointer to char at that allocated space. Then you can copy the string to that allocated space via the address of it (which is the pointer to it).

    What you are doing when you say names[++idx] = line; is assigning the address of the char array 'line' to the POINTER names[idx]. This means if the line array changes, names[idx] will reflect those changes (because it is just storing the ADDRESS of names[idx], not the VALUE or string itself).

    Another big problem is the fact that you only declare the "names" pointer array to be of size 2. You then read into that array in a while() loop an UNLIMITED amount of lines.

    As soon as you read more then 2 lines you are writing into memory you don't have proper access to and will cause undefined behavior. Any time you use an array you need to perform STRICT bounds checking, especially if you are reading from USER supplied input or something outside of hard-coded information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings from file
    By eganWall in forum C Programming
    Replies: 2
    Last Post: 11-22-2011, 01:22 PM
  2. Reading Strings from a file.
    By RealLife in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2007, 09:17 PM
  3. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  4. Reading in strings from a file..
    By Anti_Visual in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 02:56 AM
  5. Reading strings from a file
    By Capulet in forum C Programming
    Replies: 7
    Last Post: 12-04-2002, 03:58 AM

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