0

I am trying to read - write a txt file that has several info in different lines. It is in the form of:

Number-LicencePlate NumberOfSeats

Name  number  phonenumber

Name  number  phonenumber

Name  number  phonenumber

To read the first line it is pretty easy using fscanf But how can I read the rest of it using fscanf to obtain 3 different variables (Name,Number,phone)?

Write to this file in the same form comes at a later stage but will try to work it out..

FILE *bus;
bus = fopen ("bus.txt","r");
if (bus == NULL)
{
    printf("Error Opening File, check if file bus.txt is present");
    exit(1);
}
fscanf(bus,"%s %d",platenr, &numberofseats); 
printf("Bus Licence plate Nr is: %s and number of seats is: %d", platenr, numberofseats);
Lella
  • 256
  • 8
  • 14
baskon1
  • 330
  • 1
  • 3
  • 15
  • 2
    Tons of duplicates here: https://www.google.com/search?q=how+to+read+text+file+using+fscanf+site%3Astackoverflow.com – alk Dec 17 '16 at 12:25
  • Use a while loop! – kiner_shah Dec 17 '16 at 13:34
  • Possible duplicate of [In C, how should I read a text file and print all strings](http://stackoverflow.com/questions/3463426/in-c-how-should-i-read-a-text-file-and-print-all-strings) – kiner_shah Dec 17 '16 at 13:48
  • I can use a while loop, but how would I skip the first line to loop through the rest of the file? I have found that it can be done this way: fscanf(config_file, "%*[^\n]\n", NULL); – baskon1 Dec 17 '16 at 13:56
  • Write good error messages: `char *path = "bus.txt"; bus = fopen(path, "r"); if(bus==NULL){ perror(path); exit(1);}` The system error is important (does the file not exist, or is it a permission issue? Don't make the user guess, just tell them) and belongs on stderr. – William Pursell Dec 17 '16 at 15:21
  • Do not use `scanf`. If you are learning C, it is more helpful to use `fgets` and parse the data with `strtof`, `strtoll`, etc. If you know C, it is usually easier to do so. The *only* function `scanf` has is to confuse people when they are learning the language. – William Pursell Dec 17 '16 at 15:23
  • Ok thank you so much! Yes I think I have found a solution (fingers crossed), using fgets! Probably will just read the first line with fscanf and then the rest with fgets! Will update with my results! – baskon1 Dec 17 '16 at 15:27

1 Answers1

1

You should use a loop in order to achieve what you are looking for as your code is not reading anything except the first line as "FILE *bus;" is a pointer to the first line of the text file.

In order to read it all you can use a simple while loop by checking End of File(EOF). There are 2 methods that I am aware of and here they are;

  while(!feof(bus)){
       fscanf(bus,"%s %d",platenr, &numberofseats);
       printf("Bus Licence plate Nr is: %s and number of seats is: %d", platenr, numberofseats);
    }

This code block will print each line after reading it. We have used "feof ( FILE * stream );" function Learn More Here. There are also alternative ways suggested on other articles How to read a whole text file

But I will put it here that solution as well.

  while(fscanf(bus,"%s %d",platenr, &numberofseats)!=EOF){
        printf("Bus Licence plate Nr is: %s and number of seats is: %d", platenr, numberofseats);
    }

Zeromika
  • 347
  • 2
  • 12