You are here:

C/file read in

Advertisement


Question
Hi I'm having an issue with my program. I'm trying to read in the information from an input file. It's sort of something like this:

1
10 3
10:35 PM E SARAH 5
10:40 PM E DAVE 4
10:45 PM E LESLIE 6
10:50 PM E SEAN 4
11:00 PM E JENNIFER 3
12:45 AM L LESLIE
12:50 AM L JENNIFER
1:43 AM L SARAH
1:45 AM L DAVE
1:45 AM L SEAN

So far its reading in the information but the problem I'm having is, it's not reading in the time correctly. What am I doing wrong? I copied parts of my code below.

typedef struct{
       int hours;
       int minutes;
       char time[3];
       char inOut[3];
       char name[29];
       int priority;
       }PEOPLE;
       
PEOPLE *person;

int main(){

fp = fopen("input.txt", "r");
   
   //read in # of clubs
   fscanf(fp, "%d", &clubs);
   
   while(clubs != 0){
               printf("Club #%d:\n\n", number);
               
               //read in number of events
               fscanf(fp, "%d", &events);
               printf("%d", events);
               
               person = calloc(events ,sizeof(PEOPLE));
               
               //read in capacity
               fscanf(fp, "%d", &capacity);
               printf("\n%d\n", capacity);
               
               int i; char hold[3];
               for(i=0; i<events; i++){
                        fscanf(fp, "%d", &person[i].hours);
                        printf(" %d", person[i].hours);
                        //fscanf(fp, "%s", hold[i]);
                        fscanf(fp, "%d", &person[i].minutes);
                        printf(" %d", person[i].minutes);
                        fscanf(fp, "%s", person[i].time);
                         printf(" %s", person[i].time);
                        fscanf(fp, "%s", person[i].inOut);
                         printf(" %s", person[i].inOut);
                        fscanf(fp, "%s", person[i].name);
                         printf(" %s\n", person[i].name);
                         if(!strcmp(person[i].inOut, "L")){
                               fscanf(fp, "%d", &person[i].priority);
                               printf(" %d\n", person[i].priority);
                               }
                               printf("\n");
                        }
               
               
   
   
   
   
   
   
   number++;
   clubs--;
   
   
}

Answer
Hello Sue.

The problem is that fscanf is getting stuck at the colon ':' between reading hours and minutes. The fscanf reads the hours, then it expects digits for the minutes, but the colon is in the way. It can never get past it. You can fix it by changing the hours line to this:

fscanf(fp, "%d:", &person[i].hours);

Notice, I added the colon after %d

You can also read the entire line in at once like this:

fscanf(fp, "%d:%d %s %s %s %d",
    &person[i].hours,
    &person[i].minutes,
    person[i].time,
    person[i].inOut,
    person[i].name,
    &person[i].priority);

The number of spaces between items in fscanf does not matter, but all other characters need to match the input line.

You should always check the return value of any scanf function to check how many items were successfully read.

I hope that helps you out. You should be able to carry on with your work now.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.