Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read data from file but when i write it on file it wrote on file successfully but when i rem from file i give me garbage value. Plz find the Bug That i could n't detect how it not read?
The following is my code in vc++
C++
// reading_from_file.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct node
{
	int Id;
	node*next;
	node()
	{
		next=NULL;
	}
};
struct node1
{
	int id;
};
class LL
{
	node*first;

public:
	LL()
	{
		first=NULL;
	}
	void add(node*t)
	{
		if(first==NULL)
		{
			first=new node();
			first->Id=t->Id;
			first->next=NULL;
		}
		else
		{
			node*temp;
			temp=first;
			while(temp->next!=NULL)
				temp=temp->next;
			node*last=new  node();
			last->Id=t->Id;
			last->next=0;
			temp->next=last;
		}
	}
	void show()
	{
		node*tfirst=first;
		while(tfirst!=NULL)
		{
			cout<<"ID "<<tfirst->Id<<endl;
			tfirst=tfirst->next;
		}
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	node*insert=new node();
	LL obj;
	node1 ob;
	fstream fs;
	int i=1;
	fs.seekg(0);
	fs.open("Malik.txt",ios::in|ios::out|ios::binary|ios::app);
	while(!fs.eof())
	{
		fs.read(reinterpret_cast<char*>(&ob),sizeof(node1));
		cout<<"vALUE "<<(ob.id)<<endl;
		insert->Id=ob.id;
		obj.add(insert);
	}
	//while(i)
	//{
	//	//cout<<"Enter Key ";
	//	for(;i<=10;i++)
	//	{//cin>>insert->Id;	
	//		insert->Id=i*5;
	//		obj.add(insert);		
	//		fs.write(reinterpret_cast<char*>(&obj),sizeof(LL));
	//	}//cout<<"Press 0 To Terminate";
	//	//cin>>i;
	//}
	fs.close();
	//system("cls");
	obj.show();
	return 0;
}
Posted
Updated 16-Dec-11 22:47pm
v2
Comments
Richard MacCutchan 17-Dec-11 5:38am    
You did not show where you write the file or what content you expect to see when you read it back. Instead of posting all this code edit your question and just show the parts where you write and then read back your file.

1 solution

Without seeing where you are writing to the file, I can't suggest a fix, however I can suggest an alternative. Serialization[^]. The idea is that each class/struct is responsible for saving/restoring its own data, which really simplifies things.


C++
#include <stdio.h>

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;
 
struct node {
	node *next;
	int Id;
	/*This is an initialiser list, which is only valid in a constructor
	It has the same effect as "next = NULL; Id = id;", only it is a touch faster*/
	node(int id) : next(NULL), Id(id) { } //Add a parameter in here for the value to make construction neater when you make it
	void serialize(ostream &s) {
		//recursively write to file
		s.write((char *)&Id, sizeof(Id));
		if (next != NULL) {
			next->serialize(s);
		}
	}
	static node *unserialize(istream &s) {
		//recursively read from file
		int i;
		s.read((char *)&i, sizeof(i));
		if (s.eof()) {
			return NULL;
		}
		node *n = new node(i);
		n->next = unserialize(s);
		return n;
	}
};
 
class LL {
	node* first;
	//This allows us to add directly to the end of the list, instead of iterating through each element.
	//When you start getting a few hundred elements in the list you will notice the difference
	node* last;
 
public:
	LL() : first(NULL), last(NULL) { } //Another initialiser list
	~LL() {
		//Clean up the memory
		node *n = first;
		while (n != NULL) {
			node *del = n;
			n = n->next;
			delete del;
		}
	}

	void add(int i) {
		if(first == NULL) {
			first = new node(i);
			last = first;
		} else {
			//Isn't this easier?
			node *temp = new node(i);
			last->next = temp;
			last = temp;
		}
	}
	void show() {
		node *tfirst = first;
		while (tfirst != NULL) {
			cout<<"ID "<<tfirst->Id<<endl;
			tfirst = tfirst->next;
		}
	}
	void serialize(ostream &s) {
		if (first != NULL) {
			first->serialize(s);
		}
	}
	static LL *unserialize(istream &s) {
		LL *ll = new LL();
		ll->first = node::unserialize(s);
		ll->last = ll->first;
		return ll;
	}
};

int _tmain(int argc, _TCHAR* argv[]) {
	LL *ll = new LL();
	ll->add(1);
	ll->add(2);
	ll->add(4);
	ll->add(3);
	cout << "Original list:" << endl;
	ll->show();
	fstream fs;
	//NOTE: This opens the file for appending.
	//The original contents will remain in the file, and this will be added to the end
	fs.open("Malik.txt", ios::out|ios::binary|ios::app);
	ll->serialize(fs);
	delete ll;
	fs.close();
	fs.open("Malik.txt", ios::in|ios::binary|ios::app);
	cout << "Saved list:" << endl;
	ll = LL::unserialize(fs);
	ll->show();
	delete ll;
	fs.close();
	return 0;
}


Also, you could use Templates[^] for this, which would make it a little more dynamic, if you need lists of multiple data types.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900