steveo2002
Programmer
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 "sdtest.dat"
int main()
{
FILE *fp;
long idx = 0;
fp = fopen(FILE_NAME, "r"
if (fp)
{
fread(&idx, sizeof(long), 1, fp);
printf("old: %d\n",idx);
idx++;
fclose(fp);
}
fp = fopen(FILE_NAME, "w"
printf("new: %d\n",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"1A 00 00 00" as x"00 00 00 00" (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
Here's the code (it's short, I promise):
======================================
#include <stdio.h>
#define FILE_NAME "sdtest.dat"
int main()
{
FILE *fp;
long idx = 0;
fp = fopen(FILE_NAME, "r"
if (fp)
{
fread(&idx, sizeof(long), 1, fp);
printf("old: %d\n",idx);
idx++;
fclose(fp);
}
fp = fopen(FILE_NAME, "w"
printf("new: %d\n",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"1A 00 00 00" as x"00 00 00 00" (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