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;
}