Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange bug has me worried

Status
Not open for further replies.

steveo2002

Programmer
Jun 29, 2002
2
GB
Hi, I have got this incredibly strange bug happening with some code I've written and I fear there could be something fundamentaly wrong with (either me or) my system.

Here's the code (it's short, I promise):

======================================

#include <stdio.h>
#define FILE_NAME &quot;sdtest.dat&quot;

int main()
{
FILE *fp;
long idx = 0;

fp = fopen(FILE_NAME, &quot;r&quot;);

if (fp)
{
fread(&idx, sizeof(long), 1, fp);
printf(&quot;old: %d\n&quot;,idx);
idx++;
fclose(fp);
}

fp = fopen(FILE_NAME, &quot;w&quot;);

printf(&quot;new: %d\n&quot;,idx);
fwrite(&idx, sizeof(long), 1, fp);

fclose(fp);
return 0;
}

======================================

The first time I run it I get the following output:

old: 0
new: 1

which increments for each subsequent run, eg.

old: 1
new: 2

then

old: 2
new: 3

etc

BUT, once I have run it enough times for it to get to

old: 25
new: 26

the next run produces

old: 0
new: 1 !!!

The program can not read the file contents when it contains 26! It reads in the file contents of x&quot;1A 00 00 00&quot; as x&quot;00 00 00 00&quot; (verified with debugging).

I have tried this same code on a Unix system at work and it worked as you would expect, what on earth could be causing this? Any insight/advice would be very much appreiated, thanks.

Setup
-----
CPU: AMD Athlon 1GHz
OS: Win 98SE
Compiler: VC++ 6.0
 
I suppose 26 is the Ctrl-Z code, which was used as end-of-file marker in the earlier days. If this is the cuase of the problem, you should use other i-o functions.
Marcel
 
if you use &quot;fprintf&quot; and &quot;fscanf&quot; instead of &quot;fread&quot; and &quot;fwrite&quot;,it will work.

#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME &quot;sdtest.dat&quot;
void main( void )
{
FILE *fp;
long idx = 0;
fp = fopen( FILE_NAME, &quot;r&quot; );
if( fp )
{
//Set pointer to beginning of file
fseek( fp, 0L, SEEK_SET );
//Read data back from file:
fscanf( fp, &quot;%ld&quot;, &idx );
printf( &quot;old:%ld\n&quot;, idx );
idx++;
fclose( fp );
}
fp = fopen( FILE_NAME, &quot;w&quot; );
if( fp )
{
printf( &quot;new:%ld\n&quot;, idx );
fprintf( fp,&quot;%ld&quot;, idx );
fclose( fp );
}
}
 
you wont need to include <stdlib.h>,i have forgot to remove it.
 
Thanks for the responses, this problem was also happening when reading complex file strutures (where I couldn't get around it by reading/writing each variable as a character string).

But did a bit of hunting the web and found that like you suggested, when 0x1A is read it is treated as eof by code compiled with a Visual C++ compiler, IF the file was opened in text mode.

So there was the solution, to open it in binary mode (&quot;rb&quot; instead of &quot;r&quot;). I guess my compiler defaults to opening in text mode.

Thanks again, I can sleep easy now (until next time). :)
 
You should probably open the file using the &quot;rb&quot; and &quot;wb&quot; modes. This opens the file in binary format, as opposed to text format. In text format, certain &quot;character&quot; sequences are converted, while other single &quot;characters&quot;, such as 0x1a (Ctrl-Z), are interpreted differently, in this case, EOF, as was previously mentioned.

OffByOne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top