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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strage problems with fread and int=13 or 26. 1

Status
Not open for further replies.

alarcon

Programmer
Sep 3, 2001
3
0
0
DE
When I try to read an integer from a binary file and the integer is either 13 or 26 the programm doesn't read it correctly, otherwise there is no problem. I've checked and edited the file with a hex editor and it is alright. I've tried standard C and similar problems occur. I'm desperate. HELP!

This is a simple version of a function that shows what I mean.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *in, *out;
int c, rein, raus, dummy;

in=fopen("c:\\test.bin","w");
rein=13; //26;
c=fwrite(&rein, sizeof(int), 1, in);
fclose (in);

out=fopen("c:\\test.bin","r");
c=fread(&raus, sizeof(int), 1, out);
Label1->Caption=rein;
dummy=raus;
Memo1->Lines->Append(dummy);
fclose (out);
}
 
I think the problem's because u did not open the file in binary mode while reading from it...(try "rb" instead of "r")... Ankan.

Please do correct me if I am wrong. s-)
 
Greetinx!

U may replace fwrite and fread methods with fprintf and fscanf methods. Happy programming!))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top