Thread: fgets help

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    9

    fgets help

    can I start by saying I have search for hours trying to fix this but am at a total loss and any help would be more than appreciated.

    I need to read from a file and then assign the read in values to variables. Here is what I have so far.

    Code:
    #include <stdio.h>
    
    
    FILE *fr;
    
    
    
    int main(){
        fr = fopen ("planes.txt", "rb");
        char line[20][100];
        char temp[100];
        int count=0;
       while(fgets(temp,100, fr) != NULL)
       {
           line[count] = temp;
           count++;
       }
    
        fclose;
    
    }
    but i get the error

    FileInput.c:15: error: incompatible types in assignment

    I want to use the information read in later in the program.

    thanks in advance

    m1cha3l

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    the problem is in this line:

    Code:
    line[count] = temp;
    You are assigning a string to a char array. You can't do this with the =-operator.
    You should # include <string.h> and use strcpy() to copy temp into line[count].

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    9
    hilarius thank you a million times

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    You're welcome !

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What hilarius said. You should also call fclose() properly by including the file handle as its argument.

    C does not allow for strings to be assigned to an array of char. Strings are a special class in c and thus working with them is much easier making use of the proper includes.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    And last but not least: end the main() with a
    Code:
    return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM