0

I'm using fscanf in a text file like this:

T 1 1000
T 2 700
N 3 450
Y 4 200

I'm trying to count the lines to use a malloc and to do that I use:

Prize temp;
int count =0;
while ((fscanf(fa,"%c %d %f", &temp.A, &temp.B, &temp.C))!= EOF)
count ++;

where Prize is a struct:

typedef struct {
  char A;
  int B;
  float C;
} Prize;

So, after reading the lines the program prints me this:

 A: T B: 1 C: 1000.0000
 A:   B: 0 C: 0.0000
 A: T B: 2 C: 700.0000
 A:   B: 0 C: 0.0000
 A: N B: 3 C: 450.0000
 A:   B: 0 C: 0.0000
 A: Y B: 4 C: 200.0000

Using the debugger I noticed that fscanf gets (for example while reading the first line):

A = 84 'T', B=1, C=1000

and instead of reading the second line it reads another first line, but like this:

A = 10'\n', B=1, C=1000

and continues doing this for each line but the last.

I controlled the file and it doesn't have extra spaces or lines.

Any suggestions to solve the problem?

Rick
  • 21
  • 1
  • 6
  • 1
    `fscanf(fa, " %c%d%f", ...)` – melpomene Jun 26 '16 at 11:19
  • 2
    `fscanf(fa,"%c %d %f", &temp.A, &temp.B, &temp.C))` --> `fscanf(fa," %c %d %f", &temp.A, &temp.B, &temp.C))` – Sourav Ghosh Jun 26 '16 at 11:19
  • 3
    Also , `!=EOF` is not a complete solution, it must be `==3`, also. – Sourav Ghosh Jun 26 '16 at 11:20
  • 1
    "...it doesn't have excess spaces or lines" - might want to consider what causes the start of new lines, and why checking specifically for a return result of `3` with each read would probably have found this sooner. [maybe **read this**](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer). – WhozCraig Jun 26 '16 at 11:20
  • 1
    @sagivd that may unwantedly fail the last line. – Sourav Ghosh Jun 26 '16 at 11:20
  • Do you actually need to verify the format of the lines that you are counting? It is probably sufficient to count the number of newlines in the file, and for that `scanf` is the wrong tool. (It is probably sufficient to not bother counting the lines and process the data on the first pass, but that is a different issue.) – William Pursell Jun 26 '16 at 11:22
  • Not an _exact_ dupe, but [may be helpful](http://stackoverflow.com/q/29658685/2173917) – Sourav Ghosh Jun 26 '16 at 11:22

2 Answers2

1

Your file contains newlines. So the sequence of characters in the file is really as following:

T 1 1000\nT 2 700\n...

The first fscanf reads 'T', 1, and 1000. It stops at the '\n' character.

The seconds fscanf reads the '\n' character to temp.A. Now it is at the second 'T' character. Therefore, it is unable to read temp.B and temp.C

The third fscanf reads 'T', 2, and 700, but stops at '\n' again.

You should skip all whitespace characters before reading temp.A, which is done by the space in the format string:

...fscanf(fa," %c %d %f", &temp.A, &temp.B, &temp.C)...
user31264
  • 6,557
  • 3
  • 26
  • 40
0

The %c format of the reading would not ignore any space or \n character, even which it is unseen. However %s %d %c will. It is likely the better way to avoid the inconsistency if replace %c by %s(and single character to be a string).

char str[maxn];
fscanf(fa, "%s%d%f", str, &temp.B, &temp.C);
temp.A = str[0];
lulyon
  • 6,707
  • 7
  • 32
  • 49