Jump to content
  • Advertisement
Sign in to follow this  
ashgromnies

C++: Write a linked list to a binary file

This topic is 4959 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Fruny - Added source tags. [Edited by - Fruny on November 24, 2004 10:30:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
Gamedev is not the right place to ask homework questions. (gradebook is the archetypical HW assignment)

I suggest you use a std::list (in <list>) to hold your data, that will greatly simplify things.
Then write a function to write a std::string to a binary file (hint, you need to prefix it with the string size), and another to read it.

Build on that to do the same thing with a single Person.

Build on that to do the same thing with a std::list<Person> (again, prefix with the list size).

[Edited by - Fruny on November 24, 2004 11:25:09 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!