Thread: Checking for a Blank Line When Reading From a File

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    Checking for a Blank Line When Reading From a File

    So I have this text file which contains data about airports in the following format:
    Code:
    BUR Burbank
    FAT Fresno 
    LGB Long Beach
    LAX Los Angeles
    MRY Monterey Peninsula
    OAK Oakland
    SMF Sacramento
    SAN San Diego
    SFO San Francisco
    SJC San Jose
    SBO Salina
    SMO Santa Monica
    
      0   0   0   0 122   0   0   0   0   0 316   0
      0   0   0   0   0   0   0   0 321 455   0   0
      0   0   0 125   0   0   0   0 267   0   0   0
      0   0 125   0   0  50   0   0 250   0 370   0
    122   0   0   0   0 259   0   0   0   0   0 119
      0   0   0  50 259   0   0 129   0   0   0   0
      0   0   0   0   0   0   0 111   0   0 125   0
      0   0   0   0   0 129 111   0   0   0   0   0
      0 321 267 250   0   0   0   0   0 190   0   0
      0 455   0   0   0   0   0   0 190   0   0   0
    316   0   0 370   0   0 125   0   0   0   0 145
      0   0   0   0 119   0   0   0   0   0 145   0
    The first part consists of codes and the names of the airport location, the second is a chart representing flight costs from one airport to the other.

    Obviously, I cannot use the same logic to read both pieces of data, so I need to find a way to stop reading for names and codes once I reach the blank line that separates the two.

    I was thinking that I could use the strcmp() function to compare the current line to the next line and continue to scan while the current line is greater than the next (the next only containing a new line character)

    I have in mind something like:
    Code:
    char* current_line, *new_line;
    
    //current_line = ?;
    //new_line = ?;
    
    while(strcmp(current_line, next_line) == 1)
    {
        //read in data to structure I have already defined
    
    }
    Any and all feedback is appreciated

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    If you want to change your logic after encountering an empty line, then why not simply check whether the line is empty?
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Simple.

    //start
    while(this line contains something)
    do something
    //the loop has finished so the blank line has been found
    while(end of file not reached)
    do something

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    Use fgets. Check the buffer to see if it contains only a newline (or alternately, only whitespace and a newline). Or fgets + sscanf, and see if sscanf returns what you expect.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    so perhaps something along the lines of:
    Code:
        FILE *fp1;
        char buf[BUFSIZ];
        
        //open file
        
        while(fgets(buf, sizeof(buf), fp1) != NULL)
        //do something

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Maybe. How about giving it a try?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    Ok I figured it out, it's working

    Well, the first part that is.
    This is just a test but everything works fine and there's no memory leak
    Code:
    void getData ()
    {
        FILE *fp1;
        char buf[BUFSIZ];
        AIRPORT *temp;
    
        temp = createAirport();
    
        fp1 = fopen(FILE1, "r");
        if(!fp1)
        {
            printf("Error! Could not access file!");
            system("pause");
            exit(101);
        }
        //Get codes and names    
        while(strcmp(buf, "\n") != 0)
        {
            fgets(buf, sizeof(buf), fp1);
            //printf("%s\n", buf);
            sscanf(buf, "%4s%[^\n]s", temp->code, temp->name);
            printf("%s%s\n", temp->code, temp->name);
        }
        
        free(temp->name);
        free(temp);
        fclose(fp1);
        return;
    }
    Last edited by jeanermand; 02-07-2012 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 01:03 PM
  4. Replies: 3
    Last Post: 04-27-2005, 11:50 AM

Tags for this Thread