473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
6 8482
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: deko | last post by:
Can someone point me to a sample script that will log all visitors to my web site? I'm looking for a simple script that will capture IP address, Host Name, Browser, OS, and referring page - and...
3
by: seia0106 | last post by:
Hello In the course of writing a program. I have to read and write a media file(*.avi/*.mpg/*.wav) to and from memory buffer. I can put the data(from a *.avi file)in buffer successfully but when...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
17
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
4
by: welch | last post by:
while taking some rough disk performance measures on windows machines, and snooping with FileMon, i've noticed some odd behavior here's the little nul-writer i'm running: def writeTest(nBlocks,...
4
by: Nate Murray | last post by:
Hey all, I'm having a strange PHP (4.3.10) problem that seems to have something to do with javascript. This is a bit complex, so hold on to your hats. My issue is that a single function is...
5
by: sweeet_addiction16 | last post by:
hello experts...pls help me debug this code........im not able to write in the data bytes ..only the few header bytes are written... #include<stdio.h> struct mthd_chunk { char id;...
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.