Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Output A Linked List To A Binary File? (http://www.programmingforums.org/showthread.php?t=1271)

ashgromnies Nov 24th, 2004 10:36 PM

I need to read and write a linked list to a binary file so that it may be kept throughout the life of the program, but I'm having a hell of a time and my sourcecode doesn't work and I can't figure it out.
PLEASE HELP!
main.cpp:
:

#include "stdafx.h"
#include<iostream> // For cin, cout
//#include<windows.h> // For Message box stuff
#include<fstream> // For ofstream operations
#include<ctype.h> // For data validation
#include <conio.h> //for getch()
void write();
void read();
int ctr=0;//for lazy programming
int _tmain(int argc, _TCHAR* argv[])
{

int choice=0;
read();
while(choice!=4){
cout<<"Welcome to my program!\n";
cout<<"1. Add a person\n";
cout<<"2. Show everyone\n";
cout<<"3. Delete someone\n";
cout<<"4. Quit\n";
cout<<"?:\n";
cin>>choice;
if(choice==1){
//node::display();
person* a = new person();
} else if(choice==2){
node::display();
} else if(choice==4){
break;
} else if(choice==3){
int who;
cout<<"\nWho? ";
cin>>who;
node *thisguy=node::head;
if(who==0)
{
node::head=node::head->next;
who++;
}

int x=0;
while(x!=who-1){
x++;
thisguy=thisguy->next;
}
if(thisguy!=NULL){
cout<<"Person "<<x<<endl;
thisguy->next->show();
thisguy->next=thisguy->next->next;
cout<<"has been erased.\n";}


} else {
cout<<"\nWhat?\n";
}
}
write();


}
void read(){
ifstream in("stus.dat",ios::binary|ios::in);
if(!in){cout<<"no file";}
//else {
// in.read((char*)(new person(0)),sizeof(person));
// ctr++;
//}while(!in.eof())
{
person* one = new person(0);
in.read((char*)(one),sizeof(person));
ctr++;
}
in.close();
}
void write(){
ofstream out("stus.dat",ios::binary|ios::out);
node* thisguy=node::head;
int a=0;
//while(a!=ctr){
//a++;
//thisguy=thisguy->next;
//}
while(thisguy!=NULL){
//int x=0;
//while(x<ctr){
//x++;
out.write((char*)thisguy,sizeof(person));
thisguy=thisguy->next;
}
out.close();
}


node.cpp:
#include "stdafx.h"
node* node::head = NULL;
node* node::tail = NULL;
int node::num = 0;
node::node()
{

next = NULL;
if(!head){
head=this;

}
else{
tail->next=this;

}
tail=this;

}

void node::display(){
num=0;
node* t = head;
while(t)
{t->show();
t=t->next;
num++;
}
}
void node::show(){
}



person.cpp:
#include "stdafx.h"

person::person()
{

cout<<"\nWhat is their name? ";
cin>>name;
cout<<"\nWhat is their age? ";
cin>>age;
cout<<"\nWhat is their GPA?";
cin>>gpa;


}

person::person(string n, int a,double g)
{
name=n;
age=a;
gpa=g;
}
//prints out all their data
void person::show()
{
cout<<"Number: "<<setw(10)<<node::num<<endl;
cout<<"Name: "<<setw(10)<<name<<endl<<"Age: "<<setw(10)<<age<<endl;
cout<<"GPA: "<<setw(10)<<gpa<<endl<<"-----------------------"<<endl<<endl;
}
person::person(int a)
{}

stdafx.cpp:
// stdafx.cpp : source file that includes just the standard includes
// animalsanddogs.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file



node.h:
#include "stdafx.h"
#include <string>
using namespace std;
class node
{
private:

static node* tail;

public:
static node* head;
node *next;
virtual void show();
static void display();
static int num;
node();
};


person.h:
#include "stdafx.h"
#include <string.h>

using namespace std;
class person : public node//it's a node
{
private:
string name; //the name
int age; //the age
double gpa;
public:
person(); //default constructor
person(string n, int a,double g);
person(int a);
void show();

};

stdafx.h:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once
//#parma

#include <string.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <tchar.h>
#include "node.h"
#include "person.h"
using namespace std;


Mjordan2nd Nov 24th, 2004 10:56 PM

CODE tags added.

Eggbert Nov 25th, 2004 8:22 AM

>in.read((char*)(one),sizeof(person));
>out.write((char*)thisguy,sizeof(person));
This is bad on multiple levels. When working with raw binary I/O you should remember two things: read and write for non-trivial classes will fail, and read and write for structures that contain pointers will fail. The first problem stems from the difficulty of converting an object to a reasonable sequence of bytes. The second problem is caused by the indirect nature of pointers, but the shallow reading and writing of raw I/O. When you write a pointer to file, you are writing the address that is the content of the pointer, not the data that are the contents of the address pointed to. Because this address is unique to the program being run, it will not be meaningful when read from the file by another program.

To write a linked data structure such as a linked list to file, you need to do it manually by either writing only the data and rebuilding the list when you read the file, or by working out some way of packing the data structure so that it can be easily unpacked back into the original structure.


All times are GMT -5. The time now is 1:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC