Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fread problem in for loop

Status
Not open for further replies.

grexroad

Programmer
Jan 30, 2008
7
0
0
US
I am having a problem with the second for loop that is reading data in to my structure. I have the first for loop reading in data for equipment but each piece of equipment has parts that go with it and that is captured in the second for loop and put into the Parts structure. The code all works good except that once the binary file is complete, the only data I have in the Parts structure is from the last loop of the "Y" for loop. I thought about nesting the Parts structure inside of the Record structure but could not figure out the code to accomplish that task. Surely could use some help here. I actually tried to nest it and kept getting errors because of the way I used the fread. It looked something like this:

fread(&Test[x].Each[y], sizeof(struct Record), 1, f_Ptr);

inside of the Record structure I added -struct Parts Each;-

#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
#using <mscorlib.dll>

#define NUM 6

struct Parts
{

long int s_PtNm;
char s_PtDs[25];
float s_PtCt;
int s_Used;

};

struct Record
{

int s_Nums;
char s_Desc[25];
float s_Cost;
float s_Time;
int s_Made;
int s_Part;

};
int main()
{

FILE *f_Ptr;

struct Record Test[NUM];
struct Parts Each[NUM];

float a_Cost_Piece [6];
float a_Cost_Parts [6];
float a_Cost_Assembly[6];
float a_Cost_Produced[6];

float f_Item_Cost = 0;
float f_Parts_Cost = 0;
float f_Cost_Assembly = 0;

f_Ptr = fopen("V.DAT","rb");

for (int x = 0; x < NUM; x++)
{

fread(&Test[x], sizeof(struct Record), 1, f_Ptr);

printf("\n Product Stock Number: %i ", Test[x].s_Nums);
printf("\n Product Description : %s ", Test[x].s_Desc);
printf("\n Other Material Cost : %.2f ", Test[x].s_Cost);
printf("\n Assembly Line Time : %.2f ", Test[x].s_Time);
printf("\n Total Stock produced: %i ", Test[x].s_Made);
printf("\n Number of Parts Used: %i \n", Test[x].s_Part);

***below here is the area of concern***

for(int y = 0; y < Test[x].s_Part; y++)
{

***Problem here***
fread(&Each[y], sizeof(struct Parts), 1, f_Ptr);


printf("\n\tStock Part Number: %d ", Each[y].s_PtNm);
printf("\n\tParts Description: %s ", Each[y].s_PtDs);
printf("\n\tStock Part Cost : %.2f ", Each[y].s_PtCt);
printf("\n\tNumber Needed : %i \n", Each[y].s_Used);

a_Cost_Piece[x] = Each[y].s_PtCt * Each[y].s_Used;

a_Cost_Parts[x] += a_Cost_Piece[x];

}

f_Cost_Assembly = (a_Cost_Parts[x] + Test[x].s_Cost + (Test[x].s_Time * .1141));

a_Cost_Assembly[x] = f_Cost_Assembly + (f_Cost_Assembly * .16);

a_Cost_Produced[x] = a_Cost_Assembly[x] * Test[x].s_Made;

printf("\n Parts cost = $%.2f", a_Cost_Parts [x]);
printf("\n Assembly cost = $%.2f", a_Cost_Assembly[x]);
printf("\n Produced cost = $%.2f", a_Cost_Produced[x]);

printf("\n __________________________________________\n\n");

}

fclose(f_Ptr);

return 0;

}

 
Can you explain what you mean when you say the only data that you have in the Parts structure is from the final pass of the inner loop?

Have you checked to see how many passes through each of the loops are being done? I would also recommend putting in some checks for end-of-file conditions so that you can break out of the loops as necessary, rather than pass through the outer loop a guaranteed 6 times and also pass through the inner loop Record.s_Part times for outer loop pass.

Because you are reading the input file directly into the structures, I would also pay particular attention to the structure alignment setting in your project, given that no #pragma pack directives are present in your code. Depending on your alignment setting, the addresses of the structure members may not necessarily follow on 1 byte from the address of the previous member in the structure. For example, in the Parts structure, the s_PtCt member may not necessarily follow directly after the s_PtDs member. The compiler may effectly pad out a number of bytes so that s_PtCt starts on a particular word boundary or similar. How is the input file being populated?
 
Try adding a "super record" structure.

struct SuperRecord
{
Record s_Record ;
Parts s_Parts[ NUM_PARTS ] ;
}
struct SuperRecord Test[NUM];

fread(&Test[x].s_Record, sizeof(struct Record), 1, f_Ptr);

fread(&Test[x].s_Parts[y], sizeof(struct Parts), 1, f_Ptr);

I think there's a compiler or linker directive to "pack" your structures to prevent the padding the previous answer mentions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top