O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code Sample 12.7: Reading Individual Members of a union from a File

Conversely, consider the snippet for reading in data from the file:

hFile = fopen( "ulw.txt", "r" );
fread( &uLongWord.word.low, sizeof(int), 1, hFile);
fread( &uLongWord.word.high, sizeof(int), 1, hFile);
fclose (hFile);

In the previous sample, you can see that the members are read in individually as integer values. Subsequently, the result (as checked by a printf statement in the source code) is identical in value to the original, because the two-word union members overlap.

This brings you to a final example, and something that is useful for readers working with record data from operating systems that use a record structure that is not delimited. A common approach to record ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required