943,808 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1714
  • C++ RSS
Apr 12th, 2006
0

Error with taking input from a file

Expand Post »
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)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Apr 12th, 2006
0

Re: Error with taking input from a file

Quote 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)
  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;
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Apr 12th, 2006
0

Re: Error with taking input from a file

You should delete anything you dynamically allocate with new.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Apr 13th, 2006
0

Re: Error with taking input from a file

Apart from deallocating meomory , I think there should be a reason for this code not to work.
Thanks,
comwizz
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: MFC vs WIN forms - Which road to take
Next Thread in C++ Forum Timeline: Assistance Required





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC