Help how to store a linklist in a binary file

hi all
please help me how i set a linklist and finally it stored in a file and how to read that file show.
here are my code
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;
	string n;
public:
	A*next;
	A()
	{
		a=0;
		n="name";

	}
	void set()
	{
		cin>>a>>n;
		next=NULL;
	}
void show()
{
	cout<<a<<endl<<n<<endl;
}
};
int main()
{
	A*first,*last;
	first=last=NULL;
	int n;
	do{
		cout<<"press 1.setrecord\n2.show record\n0.exit\n";
		cin>>n;
		switch(n)
		{
		case 1: {
			A*R;
			R=new A;
			R->set();
			if(first==NULL)
				first=last=R;
			else
			{
				last->next=R;
				last=R;
			}
				}break;
		case 2: {
			A*temp=first;
			int flag=0;
			do
			{
				ifstream in("A.txt",ios::binary|ios::in);
		in.read(reinterpret_cast<char*>(&temp),sizeof(A));

			if(in.is_good())
			{
				temp->show();
				flag++;
			}

			}while(temp!=NULL);
			
		ifstream in("A.txt",ios::binary|ios::in);
		in.read(reinterpret_cast<char*>(temp),sizeof(A));
			while(in.)
			if(flag==0)
			 cout<<"file not found\n";
				}break;
		case 0: break;
		default: cout<<"wrong choice\n";
		}
		
		
		system("cls");
	}while(n!=0);
	fstream out("A.txt",ios::binary|ios::app|ios::out);
	
		while(first!=NULL)
		{
			
			out.write(reinterpret_cast<char*>(first),sizeof(A));
			first=first->next;
		}

}



Last edited on
Please format your code in the code tags: You'll find them under the Format: box.
Your code does not compile.

Following line numbers are approximate.

Line 54: is_good() should be good() or just if (in)

Line 62: while (in.) makes no sense. Remove the period.

You can't write out objects that contain pointers. When you read those objects back in the pointers won't be valid.

Note that string is an object that contains a pointer to the data. When you try to write out a string in the manner you are, your will get the string object, but not the data that is contained by the string object.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


thanks AbstractionAnon very help full sugestion. before this I did not know how to write in this forum.
A linked list is useful for your program, but less so for a file.

It is still a sequenced list of things, though, so as long as you can unambiguously write individual items to file, you can write them as a list in any way you wish.

For example, I may have a file with the following list of words:

apple banana pear strawberry mango


This list is easy to read and write to file -- each item is clearly delineated -- and I can load it into memory however I want. I could load it as a linked list of fruits.

Likewise, after I have loaded it (and possibly modified it) I can write the new list back out to file as a simple list of individual items.

I hope this made sense.
Topic archived. No new replies allowed.