-1

I need to use scanf to read a text file input into stdin. I want to read the text file line by line and then assign each values in the line to their values.

This is the text file
38 5 35 3099 48 222 16 4 1 Ronaldo 2 33 2572 22 97 7 6 0 Neymar 1 38 3375 43 191 18 4 0 Messi 13 37 2472 22 80 1 6 0 Griezmann

The first line is the number of max games. Then I want to read the next lines and assign it to the variables. Like number = 5, goals = 35, .... Then on to the next line and read the same way. Number = 2, goals = 33, ....

How would I do this?

jingo
  • 13
  • 1
  • 2
  • 1
    [`fscanf`](http://en.cppreference.com/w/c/io/fscanf) would work fine in a case like this, with a fixed format for each line. You should also read about structures and loops. – Some programmer dude Sep 22 '15 at 12:00
  • If you consider that a line ends before the `'\n'`, then `scanf()` can work. If you consider that a line ends with `'\n'`, use `fgets()` as typical usage of `scanf()` leaves the `'\n'` in `stdin`. – chux - Reinstate Monica Oct 01 '15 at 17:44

2 Answers2

4

You can use fscanf to read from the text file (as you have fixed format of data) -

char name[100];
int a,b,c,d,e,f,g,h;           // sorry for such variable names 
while(fscanf(fp,"%d %d %d %d %d %d %d %d %99s",&a,&b,&c,&d,&e,&f,&g,&h,name)==9)   // fp be file pointer
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • @BLUEPIXY Thanks !! :) – ameyCU Sep 22 '15 at 12:18
  • can I just use scanf? – jingo Sep 22 '15 at 13:27
  • You want to read from text file right . `fscanf` is similar to `scanf` just it reads from file . – ameyCU Sep 22 '15 at 13:28
  • I don't think so as I would enter program.exe < text.txt – jingo Sep 22 '15 at 13:30
  • @jingo Yes than certainly you can use `scanf ` . Then just remove the file descriptor(`fp`) that is present in `fscanf` . Other than that no changes are required . – ameyCU Sep 22 '15 at 13:32
  • Do you know how I can only print the first input but not the rest? – jingo Sep 22 '15 at 14:29
  • @jingo Sorry I misinterpret it . Ignore my first comment . – ameyCU Sep 22 '15 at 14:37
  • @jingo Yes to print only first line , just call `scanf` once (**without loop**) and it will read only first line and stop . Then you won't need loop . Or just include a `break;` statement inside loop , so after first iteration it will come out of loop . – ameyCU Sep 22 '15 at 14:39
1

Don't use scanf(), it is neither flexible nor reliable.

Rather, use fgets() to read the entire line from standard input and then tokenize the input using strtok(). This will give you a much robust algorithm.

If required, you can convert the tokens to int or double using strtol(), strtod() etc.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Even better, use [getline(3)](http://man7.org/linux/man-pages/man3/getline.3.html) or [readline(3)](http://man7.org/linux/man-pages/man3/readline.3.html) if you have them, then use `sscanf` or `strtok` or some manual parsing of the line – Basile Starynkevitch Sep 22 '15 at 12:00
  • @BasileStarynkevitch Yes, that is another option, too. :) – Sourav Ghosh Sep 22 '15 at 12:02
  • How would I use `fgets()`? I've never used it before and I don't know how. – jingo Sep 22 '15 at 12:02
  • @jingo Please see the links in my answer. :) – Sourav Ghosh Sep 22 '15 at 12:02
  • 3
    I beg to disagree. `scanf` is designed for and literally perfectly suitable for uniformly formatted input like this. It does all the tokenization automagically... *Especially* in cases where the input does not come from a user so that repetitions and other interactions are unnecessary. – Peter - Reinstate Monica Sep 22 '15 at 12:03
  • @PeterSchneider: `scanf` handles newlines like spaces. If the input is made of genuine lines which should be validated one by one, that could matter.... – Basile Starynkevitch Sep 22 '15 at 12:04
  • @PeterSchneider I agree with you, but tomorrow _if_ the input changes, please consider the amount of change would be required to keep up. – Sourav Ghosh Sep 22 '15 at 12:04
  • 3
    I suspect that code which manually tokenizes would require more work, not less. – Peter - Reinstate Monica Sep 22 '15 at 12:05
  • 1
    @PeterSchneider so how would I use `scanf()`? – jingo Sep 22 '15 at 12:12
  • Very much like @ameyCU. scanf is a versatile and powerful function. It pays hundredfold to read and re-read the man page and carefully examine examples. It comes really handy in many use cases which are hard and cumbersome to properly process without it (like read away unneeded input with a pattern etc.). – Peter - Reinstate Monica Sep 22 '15 at 13:24