Re: Very strange problem using FWRITE() to write data to a binary file
From: Martin Ambuhl (mambuhl_at_earthlink.net)
Date: 02/08/05
- Next message: E. Robert Tisdale: "Re: Casting return of malloc"
- Previous message: Thomas Matthews: "Re: [Trolling] assembly vs C language"
- In reply to: leonecla_at_yahoo.it: "Very strange problem using FWRITE() to write data to a binary file"
- Next in thread: CBFalconer: "Re: Very strange problem using FWRITE() to write data to a binary file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 08 Feb 2005 12:48:43 -0500
leonecla@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
- Next message: E. Robert Tisdale: "Re: Casting return of malloc"
- Previous message: Thomas Matthews: "Re: [Trolling] assembly vs C language"
- In reply to: leonecla_at_yahoo.it: "Very strange problem using FWRITE() to write data to a binary file"
- Next in thread: CBFalconer: "Re: Very strange problem using FWRITE() to write data to a binary file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|