简体   繁体   中英

Loading a text file into a c program

i'm looking to scan the file but it have two different structures.

File:

ParisRoubaix "Marco MARCATO" 33 UAD ITA 26 5:43:31 
ParisRoubaix "Sam BEWLEY" 30  ORS NZL DNF 0 

Code:

fscanf(filepointer, " %[a-zA-Z] %[a-zA-Z\" ] %d %[a-zA-Z] %[a-zA-Z] %[a-zA-Z1234567890]", )

But i have no idea how to do the finishing time there is two ending

  • hours:minutes:seconds
  • 0

So have do i scan the time of the driver, how do i do that? <3

Here is a way:

 int ret,hour,min,sec;

 while(ret =  fscanf(filepointer, " %[a-zA-Z] %[a-zA-Z\" ] %d %[a-zA-Z] %[a-zA-Z] %[a-zA-Z0-9] %[0-9] : %[0-9] : %[0-9]", ... , &hour, &min , &sec ) )
 {
    if(ret == EOF)
    {
        break;
    }
    else if(ret == 9)
    {
        // Time is there and it is stored in hour, min and sec
    }else if(ret == 7)
    {
        // 0 is in the line and 0 is stored in hour
    }else
    {
       // error in reading the file.
        printf("%d",ret);
        break;
    }
 }

here, it is important to understand what ret variable is:

  • EOF, if the pointer is reached end of file.
  • 0, if no input matched with the variable
  • number of matched variables with the file input in integer

Note: It is good practice to write %[a-zA-Z0-9] instead of %[a-zA-Z1234567890]

Below is a program that will process your file idiomatically. Conventionally, %s represents a string. It's better to be liberal in what you accept when it comes to parsing with scanf. It's easier to enforce rules on input you've already read than to understand why scanf returned an unexpectedly small value. Besides, if your rule is anything more complicated than "upper case only", you'll soon want regex (3).

In your case, you can parse the time with one pattern. If fscanf returns 9 elements, you have a real time; if 7, you have a zero. Anything else is an error. If your input has many lines, you may want to keep track of the line number for error messages. :-)

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
int
main( int argc, char *argv[] ) {
  char id[64], name[64], thing2[8], thing3[8], seconds[8];
  int ret, thing1, hour=25, min, sec;
  FILE *input;

  if( argc < 2 ) {
    errx(EXIT_FAILURE, "filename?");
  }
  if( (input = fopen(argv[1], "r")) == NULL ) {
    err(EXIT_FAILURE, "could not open %s", argv[1]);
  }
  while( (ret = fscanf(input, "%s %[a-zA-Z\" ] %d %s %s %s %d:%d:%d",
               id, name, &thing1, thing2, thing3, seconds,
               &hour, &min, &sec)) != EOF ) {
    switch(ret) {
    case 0:
      errx(EXIT_FAILURE, "nothing' doin'");
      break;
    case 9:
      printf("time is %d:%d:%d\n", hour, min, sec);
      break;
    case 7:
      printf("%s in %d seconds\n", seconds, hour);
      break;
    default: 
      errx(EXIT_FAILURE, "found %d elements", ret);
      break;
    }
  }
  if( ferror(input) ) {
    err(EXIT_FAILURE, "scan trouble"); // error in reading the file.
  }
  return EXIT_SUCCESS;  
}

Output:

$ ./time input
time is 5:43:31
DNF in 0 seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM