fread w/ Union structures 
Author Message
 fread w/ Union structures

Hi all,

I'm having a problem with this program I'm trying to write.
It uses a Union data structure as a record in a relative file. The
union is either a header for the file or a structure holding the data
to be stored in a doubly-linked list with a parallel deletion-stack.
Very complex, and even harder to describe.

Anyway, I'll cut to the chase. It's crashing when I fread for a
RECORD_T structure (the union). I'm compiling on a Unix System V system
and it compiles properly, but it complains about incompatible types
when I compile it under Linux. I tend to think Linux is correct.

Here's essentially how the whole thing is defined:

typedef struct HEADER_S
{
  int nextAvailable;
  int deletionStackHeader;
  int headOfList;
  int tailOfList;

Quote:
} HEADER_T;

typedef struct DATA_S
{
  char transactionType;
  char studentNumber[8];
  char studentName[26];
  char homeDept[8];
  char program[8];
  char year;

Quote:
} DATA_T;

typedef struct NODE_S
{
  DATA_T values;
  int next;
  int previous;

Quote:
} NODE_T;

typedef union
{
  HEADER_T fileHeader;
  NODE_T dataNode;

Quote:
} RECORD_T;

What I want to do is write a RECORD_T with the fileHeader information
at the very first part of the data file. So I fseek(relfp,0L,SEEK_SET);
to get me to the beginning and then fwrite the structure holding the
file header.
Then I write all data structures (all of which are RECORD_T's) after
that point.

To make a long story short, it appears as though fwrite and fread don't
like that Union. I know Unions aren't generally used much these days,
but it's what they're teaching in this class... dunno why. :^/
Anywho, if anyone has any ideas on how to potentially solve this
problem, it would be greatly appreciated.

Thank you,

VKube.

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Sun, 25 Nov 2001 03:00:00 GMT  
 fread w/ Union structures

Quote:

> Hi all,

> I'm having a problem with this program I'm trying to write.
> It uses a Union data structure as a record in a relative file. The
> union is either a header for the file or a structure holding the data
> to be stored in a doubly-linked list with a parallel deletion-stack.
> Very complex, and even harder to describe.

> Anyway, I'll cut to the chase. It's crashing when I fread for a
> RECORD_T structure (the union). I'm compiling on a Unix System V system
> and it compiles properly, but it complains about incompatible types
> when I compile it under Linux. I tend to think Linux is correct.

<snip>

Quote:
> What I want to do is write a RECORD_T with the fileHeader information
> at the very first part of the data file. So I fseek(relfp,0L,SEEK_SET);
> to get me to the beginning and then fwrite the structure holding the
> file header.
> Then I write all data structures (all of which are RECORD_T's) after
> that point.

> To make a long story short, it appears as though fwrite and fread don't
> like that Union. I know Unions aren't generally used much these days,
> but it's what they're teaching in this class... dunno why. :^/
> Anywho, if anyone has any ideas on how to potentially solve this
> problem, it would be greatly appreciated.

Your structures look OK to me, unless the HEADER_S and NODE_S structures
are intended to contain actual memory addresses (In which case /that's/
probably what's giving you grief.) Try posting the code that contains the
fopen(),fwrite(), and fread() invocations.

Morris Dovey
West Des Moines, Iowa USA



Sun, 25 Nov 2001 03:00:00 GMT  
 fread w/ Union structures

...

Quote:
>Anyway, I'll cut to the chase. It's crashing when I fread for a
>RECORD_T structure (the union). I'm compiling on a Unix System V system
>and it compiles properly, but it complains about incompatible types
>when I compile it under Linux. I tend to think Linux is correct.

So you appear to have a problem with your call to fread(). Unfurtunately
you didn't post the offending code so it is impossible to say what
that error might be.

...

Quote:
>typedef union
>{
>  HEADER_T fileHeader;
>  NODE_T dataNode;
>} RECORD_T;

>What I want to do is write a RECORD_T with the fileHeader information
>at the very first part of the data file. So I fseek(relfp,0L,SEEK_SET);
>to get me to the beginning and then fwrite the structure holding the
>file header.
>Then I write all data structures (all of which are RECORD_T's) after
>that point.

Sounds perfectly reasonable. The critical thing is how you do this.

Quote:
>To make a long story short, it appears as though fwrite and fread don't
>like that Union.

If you're trying to pass a union that's your problem. The first
argument of fread() and fwrite() must be a *pointer* to the object you want
to read or write. A pointer to a union would be fine.

--
-----------------------------------------


-----------------------------------------



Sun, 25 Nov 2001 03:00:00 GMT  
 fread w/ Union structures

Quote:

>Your structures look OK to me, unless the HEADER_S and NODE_S structures
>are intended to contain actual memory addresses (In which case /that's/
>probably what's giving you grief.) Try posting the code that contains the
>fopen(),fwrite(), and fread() invocations.

Sorry, I figured it out this afternoon after much tampering.
Ended up being the dumbest thing in the world.
I needed to put the & for the address of the structure.Dumb eh? This is what
I was doing:

RECORD_T tempRecord;

// Fill tempRecord with data

fwrite(tempRecord,sizeof(RECORD_T),1,relfp);

... but since that first argument is a void POINTER, I figured I had to
write it like this:

fwrite(&tempRecord,sizeof(RECORD_T),1,relfp);

Oops. :^/

Thanks anyway,

VKube.



Mon, 26 Nov 2001 03:00:00 GMT  
 fread w/ Union structures

Quote:

>I needed to put the & for the address of the structure.Dumb eh?

A blunder, but no dumber than most.

You'd normally expect the compiler to spot this particular error.  If it
didn't, you might have forgotten to include the right header file (stdio.h
for fread).  Alternatively, you might have warnings turned down too far.

Cheers,
Richard



Mon, 26 Nov 2001 03:00:00 GMT  
 fread w/ Union structures


Quote:

>>I needed to put the & for the address of the structure.Dumb eh?

>A blunder, but no dumber than most.

>You'd normally expect the compiler to spot this particular error.  If it
>didn't, you might have forgotten to include the right header file (stdio.h
>for fread).

You would need to have included <stdio.h> to get the FILE definition for
the 4th argument.

Quote:
>Alternatively, you might have warnings turned down too far.

Possibly but this is a constraint violation that I would expect to
be diagnosed at any warning level. Possibly the SYSV compiler being used
was not an ANSI compiler or not being used in an ANSI mode.

--
-----------------------------------------


-----------------------------------------



Wed, 28 Nov 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

2. fread and structures

3. fwrite,fread,File & Structures

4. ?fread fgets from a record to a structure

5. Question regarding unions and structures

6. Structures vs. Unions

7. Initialising Union members of Structures

8. Bit-fields, unions and structures...

9. structure, union & enumeration tags

10. help: unnamed unions within structures

11. Union within structures

12. Alignment of data in a Structure/Union

 

 
Powered by phpBB® Forum Software