By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
<le******@yahoo.it> wrote:
: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
: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 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.
--
Disobey all self-referential sentences!
Nov 14 '05 #2

P: n/a
le******@yahoo.it wrote:
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[i]);
if (!(tstfile = fopen(fname, "wb"))) {
printf("\"%s\" could not be opened for output.\n", fname);
exit(EXIT_FAILURE);
}
if (fwrite(&w[i], sizeof w[i], 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.


--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3

P: n/a
le******@yahoo.it wrote:

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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #4

P: n/a
On 8 Feb 2005 08:35:12 -0800,
le******@yahoo.it <le******@yahoo.it> wrote:

....
{
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
Nov 14 '05 #5

P: n/a
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

Nov 14 '05 #6

P: n/a
Villy Kruse <ve*@station02.ohout.pharmapartners.nl> writes:
On 8 Feb 2005 08:35:12 -0800,
le******@yahoo.it <le******@yahoo.it> wrote:

....
{
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.


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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.