| | |
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
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





