I’m on a Fedora 15 computer and I have a simple code that looks like this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x[50], y[50];
int i;
FILE *f_in = fopen("readtest.dat","r");
if (f_in == NULL) printf("No file...\n");
else
{
i = 0;
while (!feof(f_in))
{
fscanf(f_in,"%d %d",&x[i],&y[i]);
printf("%d %d\n", x[i], y[i]);
i++;
}
printf("I've read %d data.\n", i);
}
return 0;
}
The file to be read is this
1 1
2 2
3 3
4 4
5 5
But I don’t know why the output looks like this.
1 1
2 2
3 3
4 4
5 5
1250259108 1250140145
I've read 6 data.
I was thinking that I left a blank new line in the file, but I didn’t. I double checked the file with both gedit and vim and no blank lines were found. Why am I reading this unexisting line?
You should include a new line escape character in your
fscanfAlso dont forget to
fcloseyour fileEDIT
I tried it and here are the results.
Without the
\nWith the
\n