468,512 Members | 1,348 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

problems with fread

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?
Jan 8 '08 #1
2 5565
On Jan 8, 7:03 pm, steve005 <burr...@gmail.comwrote:
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
<news:comp.lang.c++>
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.

Jan 8 '08 #2
steve005 wrote:
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 ...

--
Er*********@sun.com
Jan 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pete | last post: by
4 posts views Thread by Jakob Møbjerg Nielsen | last post: by
2 posts views Thread by Mike | last post: by
13 posts views Thread by 010 010 | last post: by
5 posts views Thread by David Mathog | last post: by
20 posts views Thread by ericunfuk | last post: by
1 post views Thread by kirany82 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.