I need some urgent help here. This is the (isolated) problem. When I drag a file(s) onto the program's icon to run this program, fread() reads incorrect values into the variables! Otherwise, it runs fine.
The compiler is DevC++, running on Windows 98. If anyone can compile and run this program to duplicate these results, I would sincerely appreciate it.
I would be interested to see if other compilers produce the same results as well....I need to know if this is a compiler or OS bug!!!
Code:#include <iostream> #include <stdio.h> int main(int argc, char *argv[]) { const char file[] = "x.dat"; int a = 123; int b = 456; cout << "a: " << a << endl; cout << "b: " << b << endl << endl; FILE * fp = fopen(file, "rb"); if(fp) { fread(&a, sizeof(int), 1, fp); fread(&b, sizeof(int), 1, fp); fclose(fp); } else { fp = fopen(file, "wb"); if(fp) { fwrite(&a, sizeof(int), 1, fp); fwrite(&b, sizeof(int), 1, fp); fclose(fp); } } cout << "a: " << a << endl; cout << "b: " << b << endl; cin.get(); return 0; }



LinkBack URL
About LinkBacks



Again, thanks so much. I will be more observant in the future!