Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 24th, 2004, 9:36 PM   #1
ashgromnies
Newbie
 
Join Date: Nov 2004
Posts: 4
Rep Power: 0 ashgromnies is on a distinguished road
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;
ashgromnies is offline   Reply With Quote
Old Nov 24th, 2004, 9:56 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
CODE tags added.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 25th, 2004, 7:22 AM   #3
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>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.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:52 AM.

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