473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 5821
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
I've run into an interesting memory problem. I think I'm running out of heap space, but I'm not sure.... I'm creating two new arrays like such.... pImage = new UBYTE ; pImageTemp = new...
4
by: Jakob Møbjerg Nielsen | last post by:
Expat keeps telling me that there is "junk after document element". I've tried different encoding, and I'm quite sure that the buffer is nul-terminated. I really have no idea to what the problem...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
1
by: kirany82 | last post by:
Hi all, I am writign a program to get the process no(linux) into an integer. i am not able to get the process no into integer array...this is the program. please help me. #include <stdio.h>...
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

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.