| | |
Error with taking input from a file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
#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(); } } }
•
•
•
•
Originally Posted by comwizz
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.
C++ Syntax (Toggle Plain Text)
#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(); } } }
password* counter=new password;
You should delete anything you dynamically allocate with new.
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
![]() |
Similar Threads
- C++ Reading from a text file (C++)
- taking input (Java)
- Ignoring xmlns namespaces in the input xml file (XML, XSLT and XPATH)
- Getting all data from an input and output file (C++)
- Error Checking for user input (Java)
- input data from a file into an Array (C)
- Error Message Concerning Reading File From A Drive (C++)
- Error message on start up - bridge.dll file missing (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: MFC vs WIN forms - Which road to take
- Next Thread: Disable Close(X in upper right corner) button on Form
Views: 1575 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





