Very strange problem using FWRITE() to write data to a binary file

L

leonecla

Hi everybody,
I'm facing a very very strange problem with a very very simple C
program...
My goal should be to write to a binary file some numbers (integers),
each one represented as a sequence of 32 bit.

I made this stupid trial code:

---------------------------------------------
FILE *fout;
int w;

main()
{
f_out = fopen("data_8bit.bin", "wb");
if(f_out == NULL)
/* error signaling and exit */
else
{
w = 0x17070707;
fwrite(&w, sizeof(w), 1, f_out);
fclose(f_out);
}
}
---------------------------------------------

If I execute this code (compile with Visual Studio 6) and then open the
"data_8bit.bin" file (inside the Visual Studio), it is displayed as a
binary file, and I can see the data "07 07 07 17". It is correct.
Now, if in the code above I assign to w the value 0x07070707 (instead
of 0x17070707), execute the code and open the "data_8bit.bin", it
contains no more binary data...
The same happens with 0x99999999, for example.

This behavior seems extremely strange to me... I think that the
fwrite() should work with any value of 32 bit assigned to the integer
w... but instead it shows an almost "random" (to me) behavior.

Can anyone help me?
Today I lost the whole day facing this problem, without making any step
forward.


Thank you a lot.
Best regards.

Claudio
 
W

Walter Roberson

:I'm facing a very very strange problem with a very very simple C
:program...

:If I execute this code (compile with Visual Studio 6) and then open the
:"data_8bit.bin" file (inside the Visual Studio), it is displayed as a
:binary file, and I can see the data "07 07 07 17". It is correct.
:Now, if in the code above I assign to w the value 0x07070707 (instead
:eek:f 0x17070707), execute the code and open the "data_8bit.bin", it
:contains no more binary data...
:The same happens with 0x99999999, for example.

This could be an artifact of whatever Visual Studio file viewer
facility you are using -- the viewer could be looking at the first
few bytes of the file to figure out what kind of file it is
and reacting differently for different sequences.
 
M

Martin Ambuhl

Hi everybody,
I'm facing a very very strange problem with a very very simple C
program...
My goal should be to write to a binary file some numbers (integers),
each one represented as a sequence of 32 bit.

I made this stupid trial code:

Try using a declared FILE *.
---------------------------------------------
FILE *fout; ^^^^
int w;

main()
{
f_out = fopen("data_8bit.bin", "wb"); ^^^^^ f_out is not fout
if(f_out == NULL)
/* error signaling and exit */
missing statement. This is *not* your test code.
else
{
w = 0x17070707;
fwrite(&w, sizeof(w), 1, f_out);
fclose(f_out);
}
}
---------------------------------------------

If I execute this code (compile with Visual Studio 6) and then open the
"data_8bit.bin" file (inside the Visual Studio), it is displayed as a
binary file, and I can see the data "07 07 07 17". It is correct.
Now, if in the code above I assign to w the value 0x07070707 (instead
of 0x17070707), execute the code and open the "data_8bit.bin", it
contains no more binary data...
The same happens with 0x99999999, for example.

This behavior seems extremely strange to me... I think that the
fwrite() should work with any value of 32 bit assigned to the integer
w... but instead it shows an almost "random" (to me) behavior.

Can anyone help me?

Try posting code that actually demonstrates your problem. The above
*cannot* be your code.

Try the following and see what happens. If you have problems with it,
complain to Microsoft.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *tstfile;
unsigned long w[3] = { 0x17070707, 0x07070707, 0x99999999 };
unsigned long x;
const char fname[] = "data_8bit.bin";
size_t i;

printf("[output]\n");
for (i = 0; i < 3; i++) {
printf("Testing with %#010lx\n", w);
if (!(tstfile = fopen(fname, "wb"))) {
printf("\"%s\" could not be opened for output.\n", fname);
exit(EXIT_FAILURE);
}
if (fwrite(&w, sizeof w, 1, tstfile) == 1)
printf("one item written successfully\n");
else {
printf("fwrite failed.\n");
exit(EXIT_FAILURE);
}
fclose(tstfile);

if (!(tstfile = fopen(fname, "rb"))) {
printf("\"%s\" could not be opened for input.\n", fname);
exit(EXIT_FAILURE);
}
if (fread(&x, sizeof x, 1, tstfile) == 1)
printf("one item read successfully\n");
else {
printf("fread failed.\n");
exit(EXIT_FAILURE);
}
printf("The value read was %#010lx.\n\n", x);
fclose(tstfile);
}
return 0;
}


[output]
Testing with 0x17070707
one item written successfully
one item read successfully
The value read was 0x17070707.

Testing with 0x07070707
one item written successfully
one item read successfully
The value read was 0x07070707.

Testing with 0x99999999
one item written successfully
one item read successfully
The value read was 0x99999999.
 
C

CBFalconer

I'm facing a very very strange problem with a very very simple C
program...
My goal should be to write to a binary file some numbers (integers),
each one represented as a sequence of 32 bit.

I made this stupid trial code:

---------------------------------------------
FILE *fout;
int w;

main()
{
f_out = fopen("data_8bit.bin", "wb");
if(f_out == NULL)
/* error signaling and exit */
else
{
w = 0x17070707;
fwrite(&w, sizeof(w), 1, f_out);
fclose(f_out);
}
}
---------------------------------------------

If I execute this code (compile with Visual Studio 6) and then
open the "data_8bit.bin" file (inside the Visual Studio), it is
displayed as a binary file, and I can see the data "07 07 07 17".
It is correct. Now, if in the code above I assign to w the value
0x07070707 (instead of 0x17070707), execute the code and open the
"data_8bit.bin", it contains no more binary data...
The same happens with 0x99999999, for example.

I fail to see how you can execute it - it won't even compile. If
you #include <stdio.h> and correct the name for the fout variable,
and include a return 0 at the end, and put something in for the
error branch, it works just fine.
 
V

Villy Kruse

....
{
w = 0x17070707;
fwrite(&w, sizeof(w), 1, f_out);
fclose(f_out);
}
}
---------------------------------------------

If I execute this code (compile with Visual Studio 6) and then open the
"data_8bit.bin" file (inside the Visual Studio), it is displayed as a
binary file, and I can see the data "07 07 07 17". It is correct.
Now, if in the code above I assign to w the value 0x07070707 (instead
of 0x17070707), execute the code and open the "data_8bit.bin", it
contains no more binary data...

Not strange at all. Keyword here is "little endian", where the least
significant byte of a multibyte integer is stored at the lowest memory
address, that is, occurs first in the disk file.


Villy
 
C

Claudio

Walter is right, it is a problem of the Visual Studio viewer (I always
used it to display binary files, it's the first time it "betrays"
me)... In fact I downloaded a program that can display only the binary
format of files (no matter what kind of file it is), and I saw that the
data were written correctly, even those that Visual Studio didn't
display.

Sorry about the errors in the code I posted (no "stdio.h", mismatch
between "fout" and "f_out"), I did not copy&paste my code correctly (by
the way, the code I executed was without these errors).

Thank you all for your help, you saved me from losing another day of
work with this stupid problem ;-)

Best regards.
Claudio
 
K

Keith Thompson

Villy Kruse said:
On 8 Feb 2005 08:35:12 -0800,


Not strange at all. Keyword here is "little endian", where the least
significant byte of a multibyte integer is stored at the lowest memory
address, that is, occurs first in the disk file.

Endianness doesn't explain the problem he was seeing. If you write
the value 0x07070707 to a file, you should see consecutive bytes with
the value 0x07, regardless of byte order. He was seeing nothing
(which, as it turns out, was the fault of the tool he was using to
view the file).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top