B
berkay
i have a txt file;
berkay#white
jack#black
smith#jane
writes in it.
and after i run the program it only prints smith jane and crashes
what is wrong?
#include<iostream>
#include<fstream>
using namespace std;
class data{
private:
char name[20];
char surname[30];
data *next;
public:
data(){next=NULL;}
friend class list;
void display();
};
class list{
private:
data *head,*tail;
public:
list(){
head=tail=NULL;}
void displayall();
void append(data *);
void load();
};
void data::display()
{
cout<<name<<" "<<surname<<endl;
}
void list::displayall()
{
data *py;
py=head;
if(py==NULL)
{
cout<<"nothing to show"<<endl;
}
while(py!=NULL){//while py points sth reasonable
py->display();
py=py->next;
py->next=NULL;
}
}
void list::append(data *ptr){
if(head==NULL)//first element
{
head=ptr;
tail=ptr;
}
else
{
tail->next=ptr;
tail=tail->next;
tail->next=NULL;
}
}
void list::load(){
ifstream file("dosya.txt",ios::in);
char buf[50];
data *datas;
datas=new data;
while(!file.eof())
{
file.getline(buf,20,'#');
strcpy(datas->name,buf);
file.getline(buf,30,'\n');
strcpy(datas->surname,buf);
append(datas);
}
file.close();
}
void main()
{
list liste;
liste.load();
liste.displayall();
}
berkay#white
jack#black
smith#jane
writes in it.
and after i run the program it only prints smith jane and crashes
what is wrong?
#include<iostream>
#include<fstream>
using namespace std;
class data{
private:
char name[20];
char surname[30];
data *next;
public:
data(){next=NULL;}
friend class list;
void display();
};
class list{
private:
data *head,*tail;
public:
list(){
head=tail=NULL;}
void displayall();
void append(data *);
void load();
};
void data::display()
{
cout<<name<<" "<<surname<<endl;
}
void list::displayall()
{
data *py;
py=head;
if(py==NULL)
{
cout<<"nothing to show"<<endl;
}
while(py!=NULL){//while py points sth reasonable
py->display();
py=py->next;
py->next=NULL;
}
}
void list::append(data *ptr){
if(head==NULL)//first element
{
head=ptr;
tail=ptr;
}
else
{
tail->next=ptr;
tail=tail->next;
tail->next=NULL;
}
}
void list::load(){
ifstream file("dosya.txt",ios::in);
char buf[50];
data *datas;
datas=new data;
while(!file.eof())
{
file.getline(buf,20,'#');
strcpy(datas->name,buf);
file.getline(buf,30,'\n');
strcpy(datas->surname,buf);
append(datas);
}
file.close();
}
void main()
{
list liste;
liste.load();
liste.displayall();
}