Binary File

Pages: 12
Hello
Can I create a linked list Class of binary file and save it?
If so, how?
If so how I can search or add to this list ... ?
Thanks in advance ...

1
2
3
4
5
6
7
8
class Student
{
int age;
char* name;
int grade;
// so on...
};
 


and I want to add list of that class so i'll whave a list of students...
what i need to d0 ?
Last edited on
do you know how to create simple linked list?
cos if u don't it will take a time until you learn this corectly...(as it did take mine :/ )

you can create it with template or without:
if without template than create an base class which will hold 2 pointers.
for example:
1
2
3
4
5
6
7
8
class first {
        first *next, *previous;
protected:
       getNext();
       getPrevious ();
       setNext ();
       setPrevoius ();
};


now each class which you would like insert into list shall inherit "first class"
1
2
3
4
5
6
7
class Student : public first
{
int age;
char* name;
int grade;
// so on...
};



you shall create list class too...
which will manipulate with object's trought "first class"
each object will point to next and previous object using next and prevoius pointer
it will have functions insert () , remove () etc and function to save objects into file

this takes litle more code than this however here is declaration:
1
2
3
4
5
6
7
8
9
class list {
       first *head, *end;
public:
       insert ();
       remove ();
       saveToFile (first& );
       addFromFile (first&);
     //etc...
};
Last edited on
and it is supported by the binary way of progrrming?
sorry-I'm not shure, didn't try that.
I've just show u how to create list, someone else may help u with bin files.
thank you 4 helping :)
I hope to find how I can do that in binary file....
I don't get what you are saying.
Can I create a linked list Class of binary file and save it?
Do you want to create a linked list that contains files (std::fstream objects) or build a list from the information stored in some file? Also, where are you going to save it? You can't save fstreams into any file and saving what you just read doesn't make sense.
and it is supported by the binary way of progrrming?
There is no such thing as a binary way of programming. What did you mean?

I somehow have a feeling you might be talking about binary trees..
I'm just laughing.
I am not sure, but I think what he is trying to do is serialize a linked list to a file. To that, the answer is, you can't do it the same way you can an array. Due to the nature of linked lists, the only way to save them to a file is to save the information of each node to a file individually. If you want to save the node as a binary file, you can do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
. . . 

struct MyNode
{
    MyNode * next;
    int SomeData;
    int SomeMoreData;
    char EvenMoreData;
};

struct SerializedNode
{
    int someData;
    int SomeMoreData;
    char EvenMoreData;
};

void SomeFunction()
{
    MyNode * MyList;
    InitializeMyList(MyList); // declared elsewhere

    MyNode * cursor = MyList;

    FILE * f = fopen("output.bin", "wb");

    while (cursor)
    {
        SerializedNode node;

        node.SomeData = cursor->SomeData;
        node.SomeMoreData = cursor->SomeMoreData;
        node.EvenMoreData = cursor->EvenMoreData;

        fwrite((char *)(&node), 1, sizeof(SerializedNode), f);

        cursor = cursor->next;
    }

    fclose(f);
}

. . . 


A word of caution: This code assumes little endian on the part of the cpu. Don't use it if you intend your code to be portable across architectures. I leave it as an exercise to the reader to figure out how to get the data back out of the file and into a linked list.

PS. Every time I write code like this, someone always yells at me for something. So please take it easy this time. Also, I didn't really check the code, so there might be a few typos in it but the general idea is there.
mmiller235 (4) thank you a lot I think I got the point...

All I have to do is adding a new student class and saving it at a binary file, I'm not speaking about binary trees ...

so if I want to add students and saving it to a binary file I can use mmiller235 (4) way?
Yes, though you don't really need that additional SerializedNode structure (in a simpler case, anyway).
OK so now that I have a list of Students I want to serch for a specific student and change his grades for example how I do that?
You iterate through the list until you find what you need. That's how searching is..
Do you need iterating through a list explained?
look , I know how to pass over a list and find but now that i"m working on a binary file, it change somthing ???
I mean is it the same as I do that at a regular way ?

cause now I saved 4 example my list to a binary file and now my list is at a binary file and now I need to load that list and serch on it some data ... how I do that ?

thanks a lot in advance you help me very much!!!
Last edited on
If you save something in binary file, you just put a copy into it. The list you had is the same as before.
OK thanks a lot !
I will try it now hope 4 the best
:)
thank you !!!
I understood from other people's that I need to allocate a buffer and do all the insertions threw that buffer ....
Is it Bullshit?
???
help ...?
What are your problems? You don't need a buffer to insert something into a linked list.
I know that so 4 what is the buffer???
Pages: 12