problems with fread

S

steve005

Hi, I am writing a binary file with matlab that consists of a couple
hundred double values and then reading it in with c++. The problem
arises in c++ when I try and read in the files. At a specific point it
stops and wont read any more values. I use this code to open it and
read it:

if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0 );

fread(&coneAngle,sizeof(double),1,fp);
After about twenty simmilar reads it fails, returns an error and quits
reading data. I've
When I change the data it works just fine, I am opening up the file in
binary mode but I think it is hitting an EOF character or something
like that...
I work on a win32 machine, I've compiled it on a mac and it reads all
the data just fine. Is there any way to read the data on a windows
machine and get past this hiccup? I've also tried reading the same
file with ifstream in binary mode with the same results. Any ideas?
 
V

vippstar

Hi, I am writing a binary file with matlab that consists of a couple
hundred double values and then reading it in with c++. The problem
arises in c++ when I try and read in the files. At a specific point it
stops and wont read any more values. I use this code to open it and
read it:
If the problem arises with C++ why do you post on a C newsgroup? Try
if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0 );
What king of function is fopen_s()?
fread(&coneAngle,sizeof(double),1,fp);
After about twenty simmilar reads it fails, returns an error and quits
reading data. I've
When I change the data it works just fine, I am opening up the file in
binary mode but I think it is hitting an EOF character or something
like that...
Check the return value of fread() and use strerror().
I work on a win32 machine, I've compiled it on a mac and it reads all
the data just fine. Is there any way to read the data on a windows
machine and get past this hiccup? I've also tried reading the same
file with ifstream in binary mode with the same results. Any ideas?
Post in appropriate groups and use portable code.
 
E

Eric Sosman

steve005 said:
Hi, I am writing a binary file with matlab that consists of a couple
hundred double values and then reading it in with c++. The problem
arises in c++ when I try and read in the files. At a specific point it
stops and wont read any more values. I use this code to open it and
read it:

if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0 );

fread(&coneAngle,sizeof(double),1,fp);
After about twenty simmilar reads it fails, returns an error and quits
reading data. I've
When I change the data it works just fine, I am opening up the file in
binary mode but I think it is hitting an EOF character or something
like that...
I work on a win32 machine, I've compiled it on a mac and it reads all
the data just fine. Is there any way to read the data on a windows
machine and get past this hiccup? I've also tried reading the same
file with ifstream in binary mode with the same results. Any ideas?

Not enough information, but here are a few things to check:

1) How sure are you that a file written "with matlab" is
just a bunch of binary `double' values spliced end-to-end?

1a) How sure are you that the file got from its point of
origin to the system where you're reading it without being
damaged in transit?

2) If you're working in C++, you've asked your question
in the wrong newsgroup: this one's about C.

3) fopen_s() is not part of C. It appears to be some
kind of Microsoft "improvement," but there's no way for us
to be certain of this.

4) If fopen_s() fails (assuming a non-zero value indicates
failure), the code you've posted does not detect it.

5) How do you know that fread() eventually fails? The
code you've posted has no way to tell success from failure.

6) Are you sure the color of your computer's casing is
compatible with the software you're using on it? If you post
a follow-up message, be sure to tell us the color: it's ever
so much more relevant than SHOWING ACTUAL COMPLETE CODE ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Fread problem 52
fread() 2
fread() 6
fgetc() vs. fread() 8
H&S fread/fwrite 7
fread/fwrite 2 18
question about fread function 18
Scipy install Problems 1

Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,183
Latest member
BettinaPol

Latest Threads

Top