Advertisement

fopen and binary file, strange error

Started by September 04, 2000 03:45 PM
2 comments, last by Galileo430 24 years, 3 months ago
I write the phrase, "HEH" (char 4) 255(int) The binary file should look like, 48 45 48 00 FF 00 00 00 (note: this is little endian HEX) However it looks like this, 40 10 40 00 5F 01 00 00 Here is my code? struct bob { int b; char o[4]; }; FILE *filestr; // File Stream int numwriten; struct bob be; if ((filestr = fopen("test.bin", "wb"))==NULL) { cout << "Error Opening File" << endl; } be.b == 255; be.o == "HEH"; numwriten = fwrite( (void *)&be, sizeof(&be), 2, filestr); if (fclose(filestr)) { cout << "Error Closeing File" << endl; } Now what''s up with this?? I miss something? ------------------------------------------------------------ I wrote the best video game ever, then I woke up... Grandus.Com
------------------------------------------------------------I wrote the best video game ever, then I woke up...
quote: Original post by Galileo430
be.b == 255;
be.o == "HEH";


Ok there are two problems. First you only use the == when doing comparisons, like in if statements. To set a variable equal to a value you use the equal sign once, =.

So it should be:

be.b = 255;

For the second one (besides the equal sign problem) you can''t set char arrays equal to a value by using the equal sign. So you have to do:

be.o[0] = ''H'';
be.o[1] = ''E'';
be.o[2] = ''H'';
be.o[3] = '' '';

Notice that you have to use single quotes.

+AA_970+
Advertisement
Good it works.

Any suggestions on how to make it write a string so I don''t have to beak it down.
------------------------------------------------------------I wrote the best video game ever, then I woke up...
strcpy(be.o, "HEH");
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall

This topic is closed to new replies.

Advertisement