TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Paul Wankadia
from: andrew clarke
date: 1996-10-25 00:45:00
subject: Writing structs

19 Oct 96 20:41, Paul Wankadia wrote to All:

 > how do I write a struct to a stream?

If the stream is a pointer to the FILE type, you can do the following:

#include 
#include 

typedef struct
{
    int x;
    int y;
    int z;
}
MYSTRUCT;

int main(void)
{
    MYSTRUCT x;
    FILE *fp;
    
    x.x = (int) 0xAAAA;
    x.y = (int) 0xBBBB;
    x.z = (int) 0xCCCC;

    fp = fopen("myfile.dat", "wb");
    if (fp == NULL)
    {
        perror("Unable to open binary file for writing");
        return EXIT_FAILURE;
    }

    if (fwrite(&x, sizeof x, 1, fp) != 1)
    {
        perror("Unable to write to binary file");
    }

    if (fclose(fp) == EOF)
    {
        perror("An error was detected while closing the binary file");
    }

    return EXIT_SUCCESS;
}

Regards
Andrew

-- randy{at}zws.com

--- Msged/2 4.00
* Origin: Blizzard of Ozz, Melbourne, Australia (3:635/728.4{at}fidonet)
SEEN-BY: 50/99 620/243 623/630 635/503 544 728 670/218 711/409 410 413 430
SEEN-BY: 711/808 809 932 934 712/515 713/317 714/906 800/1
@PATH: 635/728 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.