473,387 Members | 1,650 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,387 software developers and data experts.

problem with file i/o in binary

I'm having a problem with reading a file that was written in binary.
This is the main structure of the data i want to write:
typedef struct ray_struct
{
double t;
vector origin; /* vector is a struct with 3 doubles x,y , z */
vector d;
int depth;
double efield_x, efield_y;

}ray;

I have two functions, compute_planar_source() writes the data and
fire_rays() reads the data:

int compute_planar_source()
{
...................

FILE *fp;
fp = fopen("rayinf", "wb");

if(fp == NULL)
{
perror("File could not be opened!");
return FAILURE
}

/* i have left out data structures for minB, maxB and radardetector
but the values are as follows:
minB.y = minB.x = minB.z = -10 a double
maxB.y = maxB.y = maxB.z= 10 a double

xcord and ycord are doubles
radardetector.ray_spacing is a double and value is 0.05 */

ray *p;

for(ycord = minB.y; ycord<= maxB.y; ycord +=
radardetector.ray_spacing )
{
for(xcord = minB.x; xcord<=maxB.x; xcord +=
radardetector.ray_spacing)
{

p = malloc(sizeof(ray));
if(p == NULL)
{
perror("malloc has failed!");
return FAILURE;
}
p->origin.x = xcord;
p->origin.y = ycord;
p->origin.z = minB.z - 1000;
/* this prints fine : printf("%f %f %f\n", p-
>origin.x, p->origin.y, p->origin.z); */
p->t = DBL_MAX;
p->d.x = 0;
p->d.y = 0;
p->d.z = 1;
p->depth = 0;
p->efield_x = radardetector.E0;
p->efield_y = 0;
fwrite(p, sizeof(ray), 1, fp);
free(p);
}

}

fclose(fp);

}

int fire_rays()
{
FILE *fp;
ray r;

fp = fopen("rayinf", "rb");

if(fp == NULL)
{
perror("File open failed");
return FAILURE;
}

while(fread(&r, 1, sizeof(ray), fp) == 1)
{
printf("\nOrigin: %f %f %f\n", r.origin.x, r.origin.y,
r.origin.z); // Doesn't print at all ??
}

}
int main()
{

int i;

i = compute_planar_source();
if(i == FAILURE)
{
printf("compute_planar_source() has failed\n");
}

i = fire_rays();
if(i == FAILURE)
{
printf(" fire_rays() has failed\n");
}

return 0;
}
Jun 27 '08 #1
1 1236

On Apr 28, 2:29 pm, Barry Schwarz <schwa...@dqel.comwrote:
It would be nice if you provided a description of your problem as part
of the discussion and not as a miniscule comment buried in the code
Well the problem was related to ray tracing. I was trying to generate
the rays and store them them on a file. The write operation was
working fine but there was a problem in reading the ray information
from the file.
If you look at your fwrite and your fread statements carefully, the
difference in the way you coded them should give you enough of a hint
to solve the problem. Otherwise, read my comment at the fread
statement.
Thanks for your help. I realize the problem was in fread().

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Weiming Liu | last post by:
Suppose to declare a enum type: enum ImageType { Binary, ByteGray, ShortGray, ... }
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
6
by: | last post by:
I am rewriting a C++ application in C#. This file has a combination of Text and Binary data. I used CFile before to read the text. If I hit a certain string that denotes the following data is...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
2
by: RP2001 | last post by:
I've serialized in various variables of various types (mainly CString, int, double) into an instantiated MFC CArchive class and saved it as a binary file. I am able to open the binary file and...
3
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write...
7
by: sturlamolden | last post by:
This question has been asked many times, and last time I was accused of spreading FUD. So now I will rather propose a solution. The reason for the problem is as follows: The binary installer...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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...
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,...

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.