Thread: Parsing pipe-delimited linesfrom a text file

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    3

    Question Parsing pipe-delimited linesfrom a text file

    After loading this text file:

    Code:
    Apex City Hotel|4|61 Grassmarket|Edinburgh|EH1 
    Hilton Edinburgh Airport|4|Edinburgh International Airport|Edinburgh|EH12 
    Novotel Edinburgh Centre|4|80 Lauriston Place|Edinburgh|EH3 
    Premier Inn Haymarket|3|1 Morrison Link|Edinburgh|EH3 
    Premier Inn Lauriston Place|3|82 Lauriston Place|Edinburgh|EH3 
    Premier Inn Leith|3|51-53 Newhaven Place - Leith|Edinburgh|EH6 
    Sheraton Grand Hotel & Spa|5|1 Festival Square|Edinburgh|EH3 
    The Caledonian|5|Princess Street|Edinburgh|EH1 
    The Glasshouse|5|2 Greenside Place|Edinburgh|EH1 
    The Hilton Edinburgh Grosvenor|4|Grosvenor Street|Haymarket|Edinburgh|EH12

    I'm trying to store the individual pieces of info in separate arrays.
    The while loop that uses sscanf() doesn't seem to work right. Nothing is displayed and the while loop itself doesn't seem to even execute. What am I doing wrong? I've been googling and messing with this for hours..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUM_OF_CHARACTERS 256
    
    int main(void)
    {
        char inputArray[NUM_OF_CHARACTERS];
        char *ptr;
        FILE *fp = fopen("OpenChampionshipHotels.txt", "r");
        fgets(inputArray, NUM_OF_CHARACTERS, fp);
        printf("%s", inputArray);
        ptr = strtok(inputArray, "|");
    
        while(ptr != NULL)
        {
            printf("%s\n", ptr);
            ptr = strtok(NULL, "|");
        }
    
        int lineCount = 1;
        char separators[] = "|";
    
        if (fp == NULL)
        {
            printf("Error Reading File\n");
            exit(1);
        }
    
        //find out how many lines are in the file.
        while(fgets(inputArray, sizeof(inputArray), fp) != NULL)
        {lineCount++;}
        
    
        // create arrays to store information
        char hotelName[lineCount][NUM_OF_CHARACTERS];
        char hotelRating[lineCount][NUM_OF_CHARACTERS];
        char hotelStreetAddress[lineCount][NUM_OF_CHARACTERS];
        char hotelCity[lineCount][NUM_OF_CHARACTERS];
        char hotelPostalCode[lineCount][NUM_OF_CHARACTERS];
    
        // copy lines from text file to array
        int currentLine = 0;
        while(fgets(inputArray, sizeof(inputArray), fp) != NULL && currentLine < lineCount)
        {
            printf("%s %s\n", currentLine, lineCount);
            sscanf(inputArray, "%[^|]|%[^|]|%[^|]|%[^|]|%s",
                   hotelName[currentLine],
                   hotelRating[currentLine],
                   hotelStreetAddress[currentLine],
                   hotelCity[currentLine],
                   hotelPostalCode[currentLine] );
    
            printf("%s\n", hotelName[currentLine]);
            printf("%s\n", hotelRating[currentLine]);
            printf("%s\n", hotelStreetAddress[currentLine]);
            printf("%s\n", hotelCity[currentLine]);
            printf("%s\n", hotelPostalCode[currentLine]);
    
            currentLine++;
        }
        fclose(fp);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    The main problem is your file pointer is pointing to the end of the file when you get to sscanf part. Read up on fseek().

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    printf("%s %s\n", currentLine, lineCount);
    printing integer var with %s format will have unpredictable results...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by vart View Post
    Code:
    printf("%s %s\n", currentLine, lineCount);
    printing integer var with %s format will have unpredictable results...
    oh, yea. that was just a temporary debugging line. should of been %i. I know.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    Thanks for the help. I got it to work now with the code below. one remaining problem is that the last line of the text file has 6 pieces of information instead of 5. "Haymarket" needs to be either omitted, I guess, or added to the previous piece of information (the address I think). What do you think I should do with that?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUM_OF_CHARACTERS 256
    
    //Apex City Hotel|4|61 Grassmarket|Edinburgh|EH1
    //Hilton Edinburgh Airport|4|Edinburgh International Airport|Edinburgh|EH12
    //Novotel Edinburgh Centre|4|80 Lauriston Place|Edinburgh|EH3
    //Premier Inn Haymarket|3|1 Morrison Link|Edinburgh|EH3
    //Premier Inn Lauriston Place|3|82 Lauriston Place|Edinburgh|EH3
    //Premier Inn Leith|3|51-53 Newhaven Place - Leith|Edinburgh|EH6
    //Sheraton Grand Hotel & Spa|5|1 Festival Square|Edinburgh|EH3
    //The Caledonian|5|Princess Street|Edinburgh|EH1
    //The Glasshouse|5|2 Greenside Place|Edinburgh|EH1
    //The Hilton Edinburgh Grosvenor|4|Grosvenor Street|Haymarket|Edinburgh|EH12
    
    
    int main(void)
    {
        char inputArray[NUM_OF_CHARACTERS];
        FILE *fp = fopen("OpenChampionshipHotels.txt", "r");
    
        int lineCount = 1;
        
        // check if the file loaded successfully 
        if (fp == NULL)
        {
            printf("Error Reading File\n");
            exit(1);
        }
    
        //find out how many lines are in the file.
        while(fgets(inputArray, sizeof(inputArray), fp) != NULL)
        {
            lineCount++;
        }
    
        // seek to the beginning of the file
        fseek(fp, 0, SEEK_SET);
    
        // create arrays to store information
        char hotelName[lineCount][NUM_OF_CHARACTERS];
        char hotelRating[lineCount][NUM_OF_CHARACTERS];
        char hotelStreetAddress[lineCount][NUM_OF_CHARACTERS];
        char hotelCity[lineCount][NUM_OF_CHARACTERS];
        char hotelPostalCode[lineCount][NUM_OF_CHARACTERS];
    
        // copy lines from inputArray to individual arrays
        int currentLine = 0;
        while(fgets(inputArray, sizeof(inputArray), fp) != NULL && currentLine < lineCount)
        {
            sscanf(inputArray, "%[^|]|%[^|]|%[^|]|%[^|]|%s",
                   hotelName[currentLine],
                   hotelRating[currentLine],
                   hotelStreetAddress[currentLine],
                   hotelCity[currentLine],
                   hotelPostalCode[currentLine] );
    
            printf("%s, ", hotelName[currentLine]);
            printf("%s, ", hotelRating[currentLine]);
            printf("%s, ", hotelStreetAddress[currentLine]);
            printf("%s, ", hotelCity[currentLine]);
            printf("%s\n", hotelPostalCode[currentLine]);
    
            currentLine++;
        }
        fclose(fp);
    
        return 0;
    }
    Last edited by JOhn12341234; 07-03-2013 at 08:25 AM.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A quick google returns:
    Hilton Edinburgh Grosvenor hotel. Grosvenor Street Haymarket, Edinburgh EH12 5EF, United Kingdom

    And you should init your line count to 0
    Code:
     int lineCount = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing through a text file
    By oopsyourhead in forum C++ Programming
    Replies: 14
    Last Post: 05-29-2012, 01:42 PM
  2. Tab-delimited Text File
    By eezstreet in forum C Programming
    Replies: 13
    Last Post: 05-18-2011, 06:18 PM
  3. Simple comma delimited text file
    By zeebo17 in forum C Programming
    Replies: 10
    Last Post: 06-23-2010, 08:05 AM
  4. Help parsing text file
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2008, 10:21 AM
  5. parsing delimited strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 12:57 PM

Tags for this Thread