468,736 Members | 1,780 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

problem with fwrite and fread, help please

2
Dear all,

I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth strange happen, the data read from a file is not the same the data I supposed to write. Since I can't view the binary data file, I don't know what's wrong.

Here is my code, please help me, Thanks a lot!

Elisa

#include <stdio.h>
#define CHANNELS 2
#define SAMPLES 3


void main()
{
FILE *fp_out, *fp_in;
short row, column;
char filename[] = "datain_test.dat";
long datain[CHANNELS][SAMPLES] =
{111111111,222222222,33333333,
44444444,5555555,6666666};
//size_t object_size = sizeof(long);
//size_t object_count = CHANNELS*SAMPLES;
size_t op_return;


printf("Initial values of array: \n");
for(row = 0; row<CHANNELS; row++)
{
for (column =0; column <SAMPLES; column++)
{
printf("%ld ", datain[row][column]);
}
printf("\n");
}

fp_out = fopen(filename,"w");

op_return = fwrite(&datain,sizeof(datain[0][0]),sizeof(datain)/sizeof(datain[0][0]),fp_out);
printf("fwrite return code = %d\n", op_return);

fclose(fp_out);

fp_in = fopen(filename,"r");

op_return = fread(&datain,sizeof(datain[0][0]),sizeof(datain)/sizeof(datain[0][0]),fp_in);
printf("read values of array from file: \n");

for(row = 0; row<CHANNELS; row++)
{
for (column =0; column <SAMPLES; column++)
{
printf("%ld ", datain[row][column]);
}
printf("\n");
}
printf("fread return code = %d\n",op_return);


fclose(fp_in);
asm(" nop");
}

and here is the output:

Initial values of array:
111111111 222222222 33333333
44444444 5555555 6666666
fwrite return code = 6
read values of array from file:
10420423 4063374 16515157
10879004 5505123 6619306
fread return code = 6
Nov 28 '06 #1
2 2799
elisa
2
I figured it out. Since I use TI C55xx DSP, so fread can only read a byte each time. More details in TI's webpage:

http://focus.ti.com/mcu/docs/mcusupporttechdocsc.tsp?sectionId=96&tabId=1502&ab stractName=spra757
Dear all,

I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth strange happen, the data read from a file is not the same the data I supposed to write. Since I can't view the binary data file, I don't know what's wrong.

Here is my code, please help me, Thanks a lot!

Elisa

#include <stdio.h>
#define CHANNELS 2
#define SAMPLES 3


void main()
{
FILE *fp_out, *fp_in;
short row, column;
char filename[] = "datain_test.dat";
long datain[CHANNELS][SAMPLES] =
{111111111,222222222,33333333,
44444444,5555555,6666666};
//size_t object_size = sizeof(long);
//size_t object_count = CHANNELS*SAMPLES;
size_t op_return;


printf("Initial values of array: \n");
for(row = 0; row<CHANNELS; row++)
{
for (column =0; column <SAMPLES; column++)
{
printf("%ld ", datain[row][column]);
}
printf("\n");
}

fp_out = fopen(filename,"w");

op_return = fwrite(&datain,sizeof(datain[0][0]),sizeof(datain)/sizeof(datain[0][0]),fp_out);
printf("fwrite return code = %d\n", op_return);

fclose(fp_out);

fp_in = fopen(filename,"r");

op_return = fread(&datain,sizeof(datain[0][0]),sizeof(datain)/sizeof(datain[0][0]),fp_in);
printf("read values of array from file: \n");

for(row = 0; row<CHANNELS; row++)
{
for (column =0; column <SAMPLES; column++)
{
printf("%ld ", datain[row][column]);
}
printf("\n");
}
printf("fread return code = %d\n",op_return);


fclose(fp_in);
asm(" nop");
}

and here is the output:

Initial values of array:
111111111 222222222 33333333
44444444 5555555 6666666
fwrite return code = 6
read values of array from file:
10420423 4063374 16515157
10879004 5505123 6619306
fread return code = 6[/quote]
Nov 29 '06 #2
horace1
1,510 Expert 1GB
try opening your files in binary mode wite and read
e.g.
Expand|Select|Wrap|Line Numbers
  1. fp_out = fopen(filename,"wb");
  2.  and
  3.  
  4. fp_in = fopen(filename,"rb");
  5.  
Nov 29 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Brady | last post: by
23 posts views Thread by FrancisC | last post: by
10 posts views Thread by cs | last post: by
6 posts views Thread by arne.muller | last post: by
30 posts views Thread by empriser | last post: by
3 posts views Thread by Harry | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.