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
2 3047
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]
try opening your files in binary mode wite and read
e.g. -
fp_out = fopen(filename,"wb");
-
and
-
-
fp_in = fopen(filename,"rb");
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brady |
last post by:
Hi,
I'm having a problem reading and writing to a file. What I'm trying
to do is read a file, modify the portion of the file that I just read,
and then write the modified data back to the same...
|
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);...
|
by: leonecla |
last post by:
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...
|
by: cs |
last post by:
if i have
typedef struct{
unsigned a;
unsigned *b;
}tp;
i would like to ask if it is ok for the "C standard 89" doing
something like this
{
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: rohit deshpande |
last post by:
i am writing a program in c,c++....... I want to read one file and write its contents using fread and fwrite functions. the program i hv written has no errors. but output file is not same as that of...
|
by: empriser |
last post by:
How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.
|
by: Harry |
last post by:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void scramble(void);
struct bmp_header
{
short int sig;
int size_bmp;
|
by: xiao |
last post by:
It always dumped when I tried to run it... But it compiles OK. What I
want to do is to do a test:
Read information from a .dat file and then write it to another file.
The original DAT file is...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |