Hello everyone,
I am having some problem in reading the data inputted in a file correctly using linked lists. I think this might be because of having pointer next of type password class.
Thanks,
comwizz.
The code is posted below. You may directly see the section if (reply==3) as that is where the input from the file is not correctly read.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
class password
{
 char *name;
 int code;
 public:
 password *next;
 void getdata(char*,int);
 friend void display(password*);
 void showdata();
};
void password::getdata(char* n,int cod)
{
 name=new char[strlen(n)+1];
 code=cod;
 if(name!=0)
 strcpy(name,n);
}
void display(password* head)
{
 password* current=head;
 while(current->next!=NULL)
 {
  current->showdata();
  current=current->next;
 }
 if(current->next==NULL)
 {
  current->showdata();
 }
}
void password::showdata()
{
  cout<<"Name"<<":"<<name<<endl;
  cout<<"Code"<<":"<<code<<endl;
}
void main()
{
 clrscr();
 password *head;
 password *current=head;
 int i=1,j=1;
 int reply=0;
 char *file_name;
 cout<<"Enter the name of the file in which you want to write"<<endl;
 cin>>file_name;
 fstream fout;
 fout.open(file_name,ios::in|ios::out);
 while(1)
 {
  cout<<"Enter name and code no , press -1 to abort, 1 otherwise ,3 for displaying from the file"<<endl;
  cin>>reply;
  if(reply==-1)
  {
   display(head);
   getch();
   cout<<"Aborting.."<<endl;
   exit(1);
  }
  if(reply==2)
  break;
  if(reply==3)
  {
   break;
  }
  char *name;
  name=new char[50];
  cout<<"Enter name:"<<endl;
  cin>>name;
  int code;
  cout<<"Enter code"<<endl;
  cin>>code;
  if(i==1)
  {
   head=new password;
   head->getdata(name,code);
   head->next=NULL;
   head->showdata();
   i++;
   fout.write((char*)head,sizeof(head));
  }
  else
  {
   current->next=new password;
   current=current->next;
   current->getdata(name,code);
   fout.write((char*)current,sizeof(current));
   fout.read((char*)current,sizeof(current));
   current->next=NULL;
 }
}
 if(reply==3)
 {
  fout.close();
  cout<<"Enter the name of the file"<<endl;
  char *name;
  cin>>name;
  fout.open(name,ios::in);
  fout.seekg(0);
  while(fout)
  {
   password* counter;
   fout.read((char*)counter,sizeof(counter));
   counter->showdata();
  }
 }
}

Recommended Answers

All 3 Replies

Hello everyone,
I am having some problem in reading the data inputted in a file correctly using linked lists. I think this might be because of having pointer next of type password class.
Thanks,
comwizz.
The code is posted below. You may directly see the section if (reply==3) as that is where the input from the file is not correctly read.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
class password
{
 char *name;
 int code;
 public:
 password *next;
 void getdata(char*,int);
 friend void display(password*);
 void showdata();
};
void password::getdata(char* n,int cod)
{
 name=new char[strlen(n)+1];
 code=cod;
 if(name!=0)
 strcpy(name,n);
}
void display(password* head)
{
 password* current=head;
 while(current->next!=NULL)
 {
  current->showdata();
  current=current->next;
 }
 if(current->next==NULL)
 {
  current->showdata();
 }
}
void password::showdata()
{
  cout<<"Name"<<":"<<name<<endl;
  cout<<"Code"<<":"<<code<<endl;
}
void main()
{
 clrscr();
 password *head;
 password *current=head;
 int i=1,j=1;
 int reply=0;
 char *file_name;
 cout<<"Enter the name of the file in which you want to write"<<endl;
 cin>>file_name;
 fstream fout;
 fout.open(file_name,ios::in|ios::out);
 while(1)
 {
  cout<<"Enter name and code no , press -1 to abort, 1 otherwise ,3 for displaying from the file"<<endl;
  cin>>reply;
  if(reply==-1)
  {
   display(head);
   getch();
   cout<<"Aborting.."<<endl;
   exit(1);
  }
  if(reply==2)
  break;
  if(reply==3)
  {
   break;
  }
  char *name;
  name=new char[50];
  cout<<"Enter name:"<<endl;
  cin>>name;
  int code;
  cout<<"Enter code"<<endl;
  cin>>code;
  if(i==1)
  {
   head=new password;
   head->getdata(name,code);
   head->next=NULL;
   head->showdata();
   i++;
   fout.write((char*)head,sizeof(head));
  }
  else
  {
   current->next=new password;
   current=current->next;
   current->getdata(name,code);
   fout.write((char*)current,sizeof(current));
   fout.read((char*)current,sizeof(current));
   current->next=NULL;
 }
}
 if(reply==3)
 {
  fout.close();
  cout<<"Enter the name of the file"<<endl;
  char *name;
  cin>>name;
  fout.open(name,ios::in);
  fout.seekg(0);
  while(fout)
  {
   password* counter=new password;
   fout.read((char*)counter,sizeof(counter));
   counter->showdata();
  }
 }
}

This is the slightly corrected version.Correction is
password* counter=new password;

You should delete anything you dynamically allocate with new.

Apart from deallocating meomory , I think there should be a reason for this code not to work.
Thanks,
comwizz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.