473,793 Members | 2,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with fwrite and fread, help please

2 New Member
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.da t";
long datain[CHANNELS][SAMPLES] =
{111111111,2222 22222,33333333,
44444444,555555 5,6666666};
//size_t object_size = sizeof(long);
//size_t object_count = CHANNELS*SAMPLE S;
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,s izeof(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 3080
elisa
2 New Member
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/mcusupporttechd ocsc.tsp?sectio nId=96&tabId=15 02&abstractName =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.da t";
long datain[CHANNELS][SAMPLES] =
{111111111,2222 22222,33333333,
44444444,555555 5,6666666};
//size_t object_size = sizeof(long);
//size_t object_count = CHANNELS*SAMPLE S;
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,s izeof(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 Recognized Expert Top Contributor
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

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

Similar topics

8
15360
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 location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
23
17860
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); fprintf(fnew, " %f", array);
6
8538
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
10
2294
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 {
6
5274
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;
1
5576
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 read file. main() of program is like this........ void main() { FILE *readfile,*writefile; unsigned long ml,mr; char ch; int noc=0,blen=0;
30
14286
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.
3
2080
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;
9
1935
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 like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
9670
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.