Error with taking input from a file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Error with taking input from a file

 
0
  #1
Apr 12th, 2006
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.
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<fstream.h>
  6. class password
  7. {
  8. char *name;
  9. int code;
  10. public:
  11. password *next;
  12. void getdata(char*,int);
  13. friend void display(password*);
  14. void showdata();
  15. };
  16. void password::getdata(char* n,int cod)
  17. {
  18. name=new char[strlen(n)+1];
  19. code=cod;
  20. if(name!=0)
  21. strcpy(name,n);
  22. }
  23. void display(password* head)
  24. {
  25. password* current=head;
  26. while(current->next!=NULL)
  27. {
  28. current->showdata();
  29. current=current->next;
  30. }
  31. if(current->next==NULL)
  32. {
  33. current->showdata();
  34. }
  35. }
  36. void password::showdata()
  37. {
  38. cout<<"Name"<<":"<<name<<endl;
  39. cout<<"Code"<<":"<<code<<endl;
  40. }
  41. void main()
  42. {
  43. clrscr();
  44. password *head;
  45. password *current=head;
  46. int i=1,j=1;
  47. int reply=0;
  48. char *file_name;
  49. cout<<"Enter the name of the file in which you want to write"<<endl;
  50. cin>>file_name;
  51. fstream fout;
  52. fout.open(file_name,ios::in|ios::out);
  53. while(1)
  54. {
  55. cout<<"Enter name and code no , press -1 to abort, 1 otherwise ,3 for displaying from the file"<<endl;
  56. cin>>reply;
  57. if(reply==-1)
  58. {
  59. display(head);
  60. getch();
  61. cout<<"Aborting.."<<endl;
  62. exit(1);
  63. }
  64. if(reply==2)
  65. break;
  66. if(reply==3)
  67. {
  68. break;
  69. }
  70. char *name;
  71. name=new char[50];
  72. cout<<"Enter name:"<<endl;
  73. cin>>name;
  74. int code;
  75. cout<<"Enter code"<<endl;
  76. cin>>code;
  77. if(i==1)
  78. {
  79. head=new password;
  80. head->getdata(name,code);
  81. head->next=NULL;
  82. head->showdata();
  83. i++;
  84. fout.write((char*)head,sizeof(head));
  85. }
  86. else
  87. {
  88. current->next=new password;
  89. current=current->next;
  90. current->getdata(name,code);
  91. fout.write((char*)current,sizeof(current));
  92. fout.read((char*)current,sizeof(current));
  93. current->next=NULL;
  94. }
  95. }
  96. if(reply==3)
  97. {
  98. fout.close();
  99. cout<<"Enter the name of the file"<<endl;
  100. char *name;
  101. cin>>name;
  102. fout.open(name,ios::in);
  103. fout.seekg(0);
  104. while(fout)
  105. {
  106. password* counter;
  107. fout.read((char*)counter,sizeof(counter));
  108. counter->showdata();
  109. }
  110. }
  111. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error with taking input from a file

 
0
  #2
Apr 12th, 2006
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.
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<fstream.h>
  6. class password
  7. {
  8. char *name;
  9. int code;
  10. public:
  11. password *next;
  12. void getdata(char*,int);
  13. friend void display(password*);
  14. void showdata();
  15. };
  16. void password::getdata(char* n,int cod)
  17. {
  18. name=new char[strlen(n)+1];
  19. code=cod;
  20. if(name!=0)
  21. strcpy(name,n);
  22. }
  23. void display(password* head)
  24. {
  25. password* current=head;
  26. while(current->next!=NULL)
  27. {
  28. current->showdata();
  29. current=current->next;
  30. }
  31. if(current->next==NULL)
  32. {
  33. current->showdata();
  34. }
  35. }
  36. void password::showdata()
  37. {
  38. cout<<"Name"<<":"<<name<<endl;
  39. cout<<"Code"<<":"<<code<<endl;
  40. }
  41. void main()
  42. {
  43. clrscr();
  44. password *head;
  45. password *current=head;
  46. int i=1,j=1;
  47. int reply=0;
  48. char *file_name;
  49. cout<<"Enter the name of the file in which you want to write"<<endl;
  50. cin>>file_name;
  51. fstream fout;
  52. fout.open(file_name,ios::in|ios::out);
  53. while(1)
  54. {
  55. cout<<"Enter name and code no , press -1 to abort, 1 otherwise ,3 for displaying from the file"<<endl;
  56. cin>>reply;
  57. if(reply==-1)
  58. {
  59. display(head);
  60. getch();
  61. cout<<"Aborting.."<<endl;
  62. exit(1);
  63. }
  64. if(reply==2)
  65. break;
  66. if(reply==3)
  67. {
  68. break;
  69. }
  70. char *name;
  71. name=new char[50];
  72. cout<<"Enter name:"<<endl;
  73. cin>>name;
  74. int code;
  75. cout<<"Enter code"<<endl;
  76. cin>>code;
  77. if(i==1)
  78. {
  79. head=new password;
  80. head->getdata(name,code);
  81. head->next=NULL;
  82. head->showdata();
  83. i++;
  84. fout.write((char*)head,sizeof(head));
  85. }
  86. else
  87. {
  88. current->next=new password;
  89. current=current->next;
  90. current->getdata(name,code);
  91. fout.write((char*)current,sizeof(current));
  92. fout.read((char*)current,sizeof(current));
  93. current->next=NULL;
  94. }
  95. }
  96. if(reply==3)
  97. {
  98. fout.close();
  99. cout<<"Enter the name of the file"<<endl;
  100. char *name;
  101. cin>>name;
  102. fout.open(name,ios::in);
  103. fout.seekg(0);
  104. while(fout)
  105. {
  106. password* counter=new password;
  107. fout.read((char*)counter,sizeof(counter));
  108. counter->showdata();
  109. }
  110. }
  111. }
This is the slightly corrected version.Correction is
password* counter=new password;
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Error with taking input from a file

 
0
  #3
Apr 12th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Error with taking input from a file

 
0
  #4
Apr 13th, 2006
Apart from deallocating meomory , I think there should be a reason for this code not to work.
Thanks,
comwizz
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC