|
-
June 28th, 2004, 05:59 PM
#1
fscanf gives unexpected data
I'm using fscanf to read data out of a file which I created by writing to the file eith fprintf. Here is an example of my code
----- writing out ------------
Code:
char idNum[30];
// here I fill the array
FILE * fout = fopen("data.txt","w");
fprintf(fout,"Id num %s\n",idNum);
fclose(fout);
----- reading in --------------
later in my program I read in the data I wrote out in a loop
testing each id for a match
Code:
char idNum[30];
FILE * fin = fopen("data.txt","r");
fscanf(fin,"Id num %s\n",idNum);
if(strcmp(idNum, desiredNum))
...
fclose(fin);
Right now I have three files to which I write the same id number each time. The id number is 97808859948941785921. The first two files I read will read in the Id num correctly, but on the third file and always the third file the id number read in is 0656663696113801664 even though when I open the file and read it myself it says 97808859948941785921. I have other functions which read the id with the same code as above just cut and pasted and it reads correctly.
Is there anything which could interfere with fscanf (and fread, I tried it and got the same result)? I am totally baffled.
Last edited by imposterrific; June 28th, 2004 at 06:13 PM.
-
June 28th, 2004, 06:24 PM
#2
It could be one of the typical "cut-and-paste" bugs: you forgot to change the name of the variable, or the literal, or something like that.
Make sure you are working with the file you think you do, and checking the value you think you check...
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
June 28th, 2004, 06:31 PM
#3
I'm afraid it's not that, I've checked it and also it reads correctly twice before it gives strange results.
I've continued testing using 6 identical files.
file 1: reads correctly
file 2: reads correctly
file 3: returns strange string
file 4: returns empty string
file 5: reads correctly
file 6: will not open file
These results make me think something screwy is going on in my system. Has anyone run into unpredictable behavior using fopen and fscanf in a Visual C++ 7.0 project which uses COM interfaces?
Last edited by imposterrific; June 28th, 2004 at 06:34 PM.
-
June 28th, 2004, 06:52 PM
#4
Originally posted by imposterrific
Has anyone run into unpredictable behavior using fopen and fscanf in a Visual C++ 7.0 project which uses COM interfaces?
First, a strange choice of API (fopen, etc.) for a Visual C++ project...
Anyway, what does it have to do with COM? Who writes those files? Do you have a timing issue (the file is not written when you are trying to read from it)?
To debug such situation, try to eliminate the unknown. If you have these 6 files, just write a program that will open and read them one by one. I am sure that would work fine. Then look: how is your current program different?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
June 29th, 2004, 11:15 AM
#5
First I will answer your questions. The same program which reads the files writes the files. There is no possibility of timing errors. All files are closed after being opened. My program does just open and read these files one by one. I can verify the contents of my files and it is not what is being read. (most of these answers were in my original post)
You say fprintf and fscanf are a strange choice, what would you suggest?
I asked about COM becuase I had read somewhere a few weeks ago that COM projects are not compatable with iostream and fstream components, and I wondered if that could be causing some sort of error.
My first and most difficult question is this: Why would two identical pieces of code read different data from the same file at different point in execution? (There are no other processes accessing these files, no multithreading, and the results are consistantly wrong, it is not random behavior.)
Last edited by imposterrific; June 29th, 2004 at 11:26 AM.
-
June 29th, 2004, 11:34 AM
#6
Let me clarify what I wrote above. I have two functions: DataLoadable(...) and LoadData(...). Each of them contains the same code
Code:
clipIn = fopen(filename,"r");
fscanf(clipIn,"ID %s\n",readID);
// then I get the actual id of the user and compare the id in the
// file with the id read from the file
if(strcmp(idNum, readID) == 0)
{
...
}
fclose(clipIn);
As a test I have made 6 copies of a data file and renamed them. My program uses a loop to call DataLoadable on all 6 of these identical files in sequence. Sometimes fscanf reads correctly, sometimes it does not. The real kicker is that LoadData will read the correct ID from the same file which DataLoadable does not. My question above (in previous post) remains.
Last edited by imposterrific; June 29th, 2004 at 12:32 PM.
-
June 29th, 2004, 11:37 AM
#7
Originally posted by imposterrific
My first and most difficult question is this: Why would two identical pieces of code read different data from the same file at different point in execution?
That's easily answered: Because an open stream maintains a current file pointer, indicating the next position to be read or written. The pointer can be moved with fseek() and rewind().
However, you would really simplify your life if you used the C++ stream classes instead of the old C file stream API.
Last edited by gstercken; June 29th, 2004 at 11:42 AM.
-
June 29th, 2004, 12:11 PM
#8
Originally posted by imposterrific
As a test I have made 6 copies of a data file and renamed them. My program uses a loop to call DataLoadable on all 6 of these identical files in sequence. Sometimes fscanf reads correctly, sometimes it does not. The real kicker is that LoadData will read the correct ID from the same file which DataLoadable does not. My question above (in previous post) remains.
Any chance you can zip up this project (including sample data files you are using) and post it here?
As for the choice of API, since we are in a Visual C++ forum, I'd use CStdioFile with its convenient ReadString() and WriteString() methods.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
June 29th, 2004, 12:13 PM
#9
One more thought.
Computers are deterministic systems: given the same input they produce the same output.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
June 29th, 2004, 12:17 PM
#10
Originally posted by VladimirF
Computers are deterministic systems: given the same input they produce the same output.
...provided they're in the same state (which is rarely the case).
-
June 29th, 2004, 12:20 PM
#11
I know this is true, that is why this problem is so baffling. My only thought is that there must be something behind the scenes interacting with the io process unbeknownst to me. (Thus my question about possible interference from COM)
I would like to stay away from CStdioFile because it is MFC.
I don't want to zip up and send the source because I am working for a company and it is not open source. Also the project may not run correctly as it is tied up will DirectShow and Dvd codecs.
Any other file io methods other than old C style, MFC, and fstream? I'm afraid those are all I know.
Last edited by imposterrific; June 29th, 2004 at 12:36 PM.
-
June 29th, 2004, 12:42 PM
#12
1) initialize your character array
2) Test the value of the file handle returned by fopen
3) Test the value returned by fscanf
Code:
char idNum[30] = { 0 };
FILE * fin = fopen("data.txt","r");
if (fin == NULL) {
DWORD err = GetLastError(); //handle error
}
else {
int numread = fscanf(fin,"Id num %s\n",idNum);
if (numread == 0 || numread == EOF) {
fscanf couldn't read the file for some reason
}
else {
if (strcmp(idNum, desiredNum) == 0) {
...
}
}
fclose(fin);
}
Perhaps you have some subtle difference in your data files such as a case mismatch or lack of a <cr> or typo in your file names or whatever that is preventing some of them from reading correctly, but is escaping visual detection.
Since you can't post your whole project, create a short program that just opens and reads these files. If the problem shows up there, post that along with the data files.
-
June 29th, 2004, 01:30 PM
#13
Well after looking very carefully I realized that I am in fact a chump. I'm really sorry guys, the two functions which gave different results were reading identical filenames but the paths were slightly different and I had residual data in the second path.
I really should have caught this and it frustrates me to no end. Sorry again.
-
June 29th, 2004, 03:12 PM
#14
Happens to most of us...
(Hopefully, no more than once).
And we had it at the very begining, remember?
"Make sure you are working with the file you think you do"
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|