Thread: Fgets problem or text file issue?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Fgets problem or text file issue?

    I'm trying to read line by line from a text file

    Code:
        FILE *fp;
        fp = fopen("airport.txt","r");
        if (fp == NULL)
        {
           exit(1);
        }
        
        char dispose[256];
        
        fgets(dispose,256,fp);
        
        printf("%s\n",dispose);
    Layout of text file: airport.txt


    Airport City/State Population Lat Lon
    BHM Birmingham,Alabama 231483 33.5639 86.7522
    ABC Los Angeles, California





    The problem is: Fgets does not stop after the 1st line. It continues reading until 256 characters are read.

    Is my textfile somehow missing a '\n'? I think Fgets cannot detect a newline char, thus it continues reading until EOF. Is it possible for a textfile to be missing newline?

    "The fgets() function reads up to the first newline it encounters, or stops when it runs out of space to store the result, or when it gets EOF (so there is no more data to read)."
    Last edited by tmac619619; 09-14-2015 at 03:59 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by tmac619619 View Post
    Is my textfile somehow missing a '\n'? I think Fgets cannot detect a newline char, thus it continues reading until EOF. Is it possible for a textfile to be missing newline?
    Yes, it's possible. For example, create a text file that only contains one long line. Some text editors will include a '\n' at the end, and some won't. Personally I think the '\n' should be there for it to be a true text file, but opinions differ.

    For your case, try opening your data file using your standard system text editor (e.g. Notepad on Windows) and make sure multiple lines are actually displayed. Using your standard system text editor will ensure that the correct newline convention for your system is used.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    How are you creating the text file? What OS are you in? In Linux, when I compile your code (with the airports.txt file created), it displays:
    Airport City/State Population Lat Lon

    Seems to work on this end. I created the file with nano. I just compiled with gcc test3.c -o test3 no warnings, no errors. I had added #include <stdio.h> and #include <stdlib.h>. Also, in your code, you're not closing the file pointer there. Make sure you do that with fclose. Thanks.

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Try adding the != NULL to the fgets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-30-2015, 08:04 AM
  2. Replies: 13
    Last Post: 12-17-2013, 07:18 PM
  3. Text file - new line issue
    By ulti-killer in forum C Programming
    Replies: 13
    Last Post: 11-04-2012, 07:30 AM
  4. Read text file issue
    By ulti-killer in forum C Programming
    Replies: 7
    Last Post: 11-01-2012, 08:01 AM
  5. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM