TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Keith Fowler
from: andrew clarke
date: 1996-04-01 01:18:12
subject: Structure and Binary

* Crossposted in area C_ECHO, AUST_C_HERE

29 Mar 96 18:22, Keith Fowler wrote to All:

 >     Hello everyone, could some body please help me.. I'm trying to
 > write a structure to a file, but i can't get it to go to the file..
 > I've tried commands like : WRITE, FWRITE, FPUTS, PUTS etc etc, but
 > with no luck.. I'm pretty sure my structure is right, here it is:

 >    typedef unsigned char byte;

 > STRUCT names {
 >     byte firstname;
 >     byte lastname;
 >     byte address;
 > };
 > STRUCT names writenames;

Umm, Keith, none of this is actually compilable C code.  :-)  I think you
should be looking at something like the following:

#include 
#include 

struct names {
    char firstname[30];
    char lastname[30];
    char address[80];
} writenames;

int main(void)
{
    FILE *fp;

    strcpy(writenames.firstname, "Andrew");
    strcpy(writenames.lastname, "Clarke");
    strcpy(writenames.address, "Melbourne, Australia");
    fp = fopen("record.dat", "wb");
    if(fp) {
        fwrite(&writenames, sizeof(writenames), 1, fp);
        fclose(fp);
    }
    return 0;
}

 >     It's probably not the best way to do it, but it works with my
 > C program (Quick C)...

Somehow I doubt that.  ;-)

 >     And also, How do i open a file for writing in BINARY mode??

fp = fopen("myfile", "wb");

 > (so it can't be edited my say, XTree Gold, when editing a Binary file
 > it should say CANNOT EDIT NON-TEXT FILE )??..

I think you'll find that as far as XTree is concerned, only files with
certain extensions (eg. .EXE, .COM, .OVL, .BIN) are treated as binary
files.  Other programs may well treat those same files differently.

Regards
Andrew

--- Msgedsq/2 3.30
* Origin: Blizzard of Ozz, Melbourne, Australia (3:635/727.4{at}fidonet)
SEEN-BY: 50/99 78/0 620/243 623/630 632/349 635/503 544 727 637/106 711/401
SEEN-BY: 711/409 410 413 430 808 809 932 934 712/515 713/888 714/906 800/1
SEEN-BY: 7877/2809
@PATH: 635/727 544 50/99 711/808 809 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™.