944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 499
  • C++ RSS
Oct 12th, 2009
0

file.close

Expand Post »
I've written a program like the follow:

C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. .
  4. .
  5. .
  6. .
  7.  
  8.  
  9. ifstream file1;
  10. ifstream file2;
  11. ofstream simiralities;
  12. simiralities.open("P(0)_P(1).txt", ios::app);
  13. file1.open("test1.txt", ios::in);
  14.  
  15. while (!file1.eof())
  16. {
  17. .
  18. .
  19. .
  20.  
  21.  
  22. file2.open("test2.txt", ios::in);
  23.  
  24.  
  25. while (!file2.eof())
  26. {
  27.  
  28. .
  29. .
  30. .
  31. .
  32.  
  33. }
  34.  
  35. file2.close();
  36.  
  37.  
  38. }
  39.  
  40. simiralities<<number;
  41. file1.close();
  42. simiralities.close();
  43.  
  44. return 0;
  45. }

the inner loop just works for the first time. from the scond time in outer loop, I can't inter the inner loop. I gusse there migth be a problem with file.close().
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hodaAgh is offline Offline
2 posts
since Oct 2009
Oct 12th, 2009
0
Re: file.close
Maybe a file2.clear() would be helpful because upon completion of the first loop the eof bit remains set.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Oct 12th, 2009
0

hey

C++ Syntax (Toggle Plain Text)
  1. 1.
  2. int main ()
  3. 2.
  4. {
  5. 3.
  6. .
  7. 4.
  8. .
  9. 5.
  10. .
  11. 6.
  12. .
  13. 7.
  14.  
  15. 8.
  16.  
  17. 9.
  18. ifstream file1;
  19. 10.
  20. ifstream file2;
  21. 11.
  22. ofstream simiralities;
  23. 12.
  24. simiralities.open("P(0)_P(1).txt", ios::app);
  25. 13.
  26. file1.open("test1.txt", ios::in);
  27. 14.
  28. // A good way to check if a file has been opened-
  29.  
  30. if (! file1)
  31.  
  32. {
  33. cout << "error opening file" << endl;
  34. return -1;
  35. /*Your program isn't opening your files correctly, and will exit here. Have a gander & see what you can come up with*/
  36. }
  37. 15.
  38. while (!file1.eof())
  39. 16.
  40. {
  41. 17.
  42. .
  43. 18.
  44. .
  45. 19.
  46. .
  47. 20.
  48.  
  49. 21.
  50.  
  51. 22.
  52. file2.open("test2.txt", ios::in);
  53. 23.
  54.  
  55. 24.
  56.  
  57. 25.
  58. while (!file2.eof())
  59. 26.
  60. {
  61. 27.
  62.  
  63. 28.
  64. .
  65. 29.
  66. .
  67. 30.
  68. .
  69. 31.
  70. .
  71. 32.
  72.  
  73. 33.
  74. }
  75. 34.
  76.  
  77. 35.
  78. file2.close();
  79. 36.
  80.  
  81. 37.
  82.  
  83. 38.
  84. }
  85. 39.
  86.  
  87. 40.
  88. simiralities<<number;
  89. 41.
  90. file1.close();
  91. 42.
  92. simiralities.close();
  93. 43.
  94.  
  95. 44.
  96. return 0;
  97. 45.
  98. }
Last edited by lexusdominus; Oct 12th, 2009 at 10:47 am. Reason: forgot code tags, init
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
lexusdominus is offline Offline
84 posts
since Jun 2009
Oct 13th, 2009
0
Re: file.close
Click to Expand / Collapse  Quote originally posted by jencas ...
Maybe a file2.clear() would be helpful because upon completion of the first loop the eof bit remains set.
It solved my problem.
thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hodaAgh is offline Offline
2 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: what is this for?
Next Thread in C++ Forum Timeline: EC++ related problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC