file.close

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 2
Reputation: hodaAgh is an unknown quantity at this point 
Solved Threads: 0
hodaAgh hodaAgh is offline Offline
Newbie Poster

file.close

 
0
  #1
Oct 12th, 2009
I've written a program like the follow:

  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().
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz
 
0
  #2
Oct 12th, 2009
Maybe a file2.clear() would be helpful because upon completion of the first loop the eof bit remains set.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

hey

 
0
  #3
Oct 12th, 2009
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: hodaAgh is an unknown quantity at this point 
Solved Threads: 0
hodaAgh hodaAgh is offline Offline
Newbie Poster
 
0
  #4
Oct 13th, 2009
Originally Posted by jencas View Post
Maybe a file2.clear() would be helpful because upon completion of the first loop the eof bit remains set.
It solved my problem.
thanx
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 247 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC