Advertisement

I hope this helps :)

Started by August 01, 2000 04:24 AM
0 comments, last by Paladin 24 years, 4 months ago
Ok, here is sourcecode (really simple) which should explain the usage of binary files: Basically, I will write two structures of car into binary file (Wolvo and Lada), and they year when those were made. This first piece of code is the code which creates data file (cool.dat): NOTICE! For some reason I didn''t get ios::bin to work, so someone told me to do it like: "ios::out | ios::binary" ------------------------------------ #include #include struct car { public: int vm; char namer[200]; }; void main(void) { car wolvo; wolvo.vm = 83; char *name = "WolVo"; strcpy(wolvo.namer,name); car lada; lada.vm = 23; name = "Latuska"; strcpy(lada.namer,name); ofstream fout("cool.dat", ios::out | ios::binary); fout.write((char *)&wolvo, sizeof(wolvo)); fout.write((char *)&lada, sizeof(lada)); fout.close(); } ------------------------------------ Now, reading: ------------------------------------ #include #include struct car { public: int vm; char namer[200]; }; void main(void) { car wolvo; car lada; ifstream fin("cool.dat", ios::in | ios::binary); fin.read((char *)&wolvo, sizeof(wolvo)); fin.read((char *)&lada, sizeof(lada)); fin.close(); printf ("vm:%d\r\n", wolvo.vm); printf ("name:%s\r\n", wolvo.namer); printf ("vm:%d\r\n", lada.vm); printf ("name:%s\r\n", lada.namer); } ------------------------------------ Something to think: For some reason you cannot actually write simply "car" structure into file, and load it with another program... I don''t know why, but it wont work. Still, you can write sub-structures into file (like, wolvo and lada). You have to define Wolvo and Lada at reading program, so I assume that you don''t need to use names "wolvo" and "lada" while you are reading, but you could name those like "car1" and "car2". I hope this helps... I learned all this just yesterday, and I checked both programs. On my Win98 & DJGPP both sources worked fine.

Here's his code, formatted a bit better.

        #include <fstream.h>#include <stdio.h>struct car        {        public:                int vm;                char namer[200];        };void main(void) {        car wolvo;        wolvo.vm = 83;        char *name = "WolVo";        strcpy(wolvo.namer,name);        car lada;        lada.vm = 23;        name = "Latuska";        strcpy(lada.namer,name);        ofstream fout("cool.dat", ios::out | ios::binary);        fout.write((char *)&wolvo, sizeof(wolvo));        fout.write((char *)&lada, sizeof(lada));        fout.close();        }------------------------------------Now, reading:------------------------------------#include <fstream.h>#include <stdio.h>struct car        {        public:                int vm;                char namer[200];        };void main(void) {        car wolvo;        car lada;        ifstream fin("cool.dat", ios::in | ios::binary);        fin.read((char *)&wolvo, sizeof(wolvo));        fin.read((char *)&lada, sizeof(lada));        fin.close();        printf ("vm:%d\r\n", wolvo.vm);        printf ("name:%s\r\n", wolvo.namer);        printf ("vm:%d\r\n", lada.vm);        printf ("name:%s\r\n", lada.namer);        }        


B e S
It's Da BOMB Baby!!!

Edited by - wrenhal on August 1, 2000 12:52:34 PM
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka

This topic is closed to new replies.

Advertisement