PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Trouble with fscanf

Ok heres my code:
Code:

int status=0;
int x=0,y=0;
FILE *input;
input=fopen(temp,"r");//r


if(!input)
{
fclose(input);
}
else{
while(status!=EOF)
{
for(x=0;x<12;x++)
{
for(y=0;y<12;y++)
{
if(status==EOF)
{return;}
else{status=fscanf( input, "%i", &level[x][y]);}
}
}
for(x=0;x<12;x++)
{
for(y=0;y<12;y++)
{
if(status==EOF)
{return;}
else{status=fscanf( input, "%i", &info[x][y]);}
}
}
for(int i=0;i<4;i++)
{
status=fscanf( input, "%i", &arrowlist[i]);
if(status==EOF){return;}
}
if(status==EOF){return;}
status=fscanf( input, "%i", &min);
if(status==EOF){return;}
status=fscanf( input, "%i", &sec);
if(status==EOF){return;}
status=fscanf( input, "%i", &cows);
//level[0][0]=5;
addlevel(level,info,arrowlist,min,sec,cows);
}
fclose(input);
}

And heres the file that I write but it wont read correctly:
5 3 3 3 3 3 3 3 3 3 3 7
1 5 7 5 7 5 7 5 7 5 7 2
5 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
8 8 6 8 6 8 6 8 6 4 4 6
8 4 4 4 4 4 4 4 4 4 4 6

100 20 0 20 0 20 0 20 0 20 0 20
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 82 0 82 0 82 0 82 0 82 0 0
0 82 0 82 0 82 0 82 0 82 0 0
0 82 0 82 0 82 0 82 0 82 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
20 0 20 0 20 0 20 0 20 0 20 0

4 3 2 1 1 2 0

5 3 3 3 3 3 3 3 3 3 3 7
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 2
8 4 4 4 4 4 4 4 4 4 4 6

0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 83 0 0 0 0 0 0 90 0 0
0 0 83 83 0 0 0 0 0 0 0 0
0 0 0 0 0 0 90 0 0 0 0 0
0 0 0 0 0 90 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0

4 3 2 1 1 2 0

My big problem is, it wont read the 0,0 part of the matrix as anything else but zero. No matter what number I put there, it always reads zero. I am sure there is not a problem with my addlevel(bla,bla) function since I can fix the matrix value of level[0][0], and info[0][0] right before the function and it will load up fine.

Has anyone else had problems with fscanf? If theres something wrong with my code, id like to know, but I personally dont see a single logical error. And get this, before I added the extra line of variables it only used to not read level0,0 and read info 0,0 JUST FINE!
What is fscanf returning when you try to read level[0][0] from the file?

Is fscanf actually writing the value 0 into level[0][0], or is the array initialized to zero and fscanf is writing nothing?

I wouldn't be surprised if fscanf is getting screwed up with what is normally referred to as 'whitespace' characters. I learned years ago to never use the scanf family of functions because they would fail and you never knew what was left in the input stream, or what the function decided to pull out or skip over.

With what you're trying to do I would just read the file into a buffer and use strtok/atoi to read the values.
When I read level[0][0] from the file it actually returns 0, I know this because I can preset level[0][0] to a number such as 5 and it will overwrite it(to zero) if I do it in the beginning of the function. Im guessing your right, it cant read the whitespace right, thing is I still want a user editable file.

I honestly have never used any file input functions besides the built in fscanf(C) or iostream(C++ and doesnt seem to be in EVC) stuff. I was wondering what file input functions you would reccomend?
Well if you want to track down the problem a bit more, change your format string in fscanf to %c and see what character is read from the beginning of the input stream.

If I were doing this I would read the entire contents of the file (using CreateFile/ReadFile) into a buffer and then parse the buffer using strtok and atoi to read the integer values.
Your code worked on my iPaq. Note that fclose will never get called because 'status' isn't set to EOF until a read fails (next time through the while loop). Other than that, it seems to read the correct data from my test file.
Use CreateFile / ReadFile functions. They are much more managable.
Its GOTTA be a compiler issue. I did fix it though, I used your buffer idea. I still used fscanf, but I read a whole levels contents of data in at a time, then set it to the matricies:
Code:
FILE *input;
input=fopen(temp,"r");//r


if(!input)
{
fclose(input);
}
else{
while(status!=EOF)
{
for(int i=0;i<295;i++)
{
if(status==EOF)
{
fclose(input);
return;
}
status=fscanf(input, "%i",&buffer[i]);
}
i=0;
for(x=0;x<12;x++)
{
for(y=0;y<12;y++)
{
level[x][y]=buffer[i];
i++;
}
}
for(x=0;x<12;x++)
{
for(y=0;y<12;y++)
{
info[x][y]=buffer[i];
i++;
}
}
for(x=0;x<4;x++)
{
arrowlist[x]=buffer[i];
i++;
}
min=buffer[i];
i++;
sec=buffer[i];
i++;
cows=buffer[i];

addlevel(level,info,arrowlist,min,sec,cows);
}
fclose(input);

Sooooo.... Somehow this works and my other code didnt. I guess it makes as much sense as when I added the last 3 lines it started not reading the info[0][0] in the first place. Thanks for pointing out the fclose part, I forgot :). I wonder what that would have ended up doing if I just left it there?

I forgot to add, THANK YOU for your help. I truly appreciate it.