Need help reading in a file

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

Join Date: Jul 2005
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Need help reading in a file

 
0
  #1
Sep 5th, 2005
Well, here is the gist of the problem. When I read in a file of integers, if there is a formatting character at the end, the last integer is repeated. For instance, in the code, it is reading i01.dat. If that file reads as follows:
2
3
4
5

with the newline character (or tab character, or whatever) at the end, then the five is printed twice when I cout the input even though I tried to use a break for the .eof() or .fail(). I have tried to use an if statement to test for \\n or \\t (i.e. if (i != \\n)) but the compiler does not understand this so I am obviously not testing properly. Please help me understand this. Here's what I've got so far.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. struct CommandLineException
  9. {
  10. CommandLineException(int max, int actual)
  11. {
  12. cout << endl << "Too many command line arguments." << endl;
  13. cout << "A maximum of " << max << " arguments are permitted." << endl;
  14. cout << actual << " arguments were entered." << endl;
  15. }
  16. };
  17.  
  18. struct FileException
  19. {
  20. FileException(char* fn)
  21. {
  22. cout << endl << "File " << fn << " could not be opened." << endl;
  23. }
  24. };
  25.  
  26. int main(int argc, char* argv[])
  27. {
  28.  
  29. try
  30. {
  31. char ifn[255], ofn[255];
  32. switch(argc)
  33. {
  34. case 1:
  35. cout << "Enter the input file name. ";
  36. cin >> ifn;
  37. cout << "Enter the output file name. ";
  38. cin >> ofn;
  39. break;
  40. case 2:
  41. strcpy(ifn, argv[1]);
  42. cout << "Enter the output file name. ";
  43. cin >> ofn;
  44. break;
  45. case 3:
  46. strcpy(ifn, argv[1]);
  47. strcpy(ofn, argv[2]);
  48. break;
  49. default:
  50. throw CommandLineException(2, argc - 1);
  51. break;
  52. }
  53. ifstream i(ifn); if(!i) throw FileException(ifn);
  54. ofstream o(ofn); if(!o) throw FileException(ofn);
  55.  
  56. int testmsg;
  57. while (true)
  58. {
  59. if (i.eof()) break;
  60. i >> testmsg;
  61. cout << testmsg << " ";
  62. }
  63.  
  64. o.close();
  65. i.close();
  66. }
  67. catch(...)
  68. {
  69. cout << "Program terminated." << endl;
  70. exit(EXIT_FAILURE);
  71. }
  72.  
  73. return 0;
  74. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 36
Reputation: Raven11 is an unknown quantity at this point 
Solved Threads: 1
Raven11 Raven11 is offline Offline
Light Poster

Re: Need help reading in a file

 
0
  #2
Sep 5th, 2005
  1. int testmsg;
  2. while (true)
  3. {
  4. if (i.eof()) break;
  5. i >> testmsg;
  6. cout << testmsg << " ";
  7. }

Try

  1. while(!eof)
or

  1. do
  2. {
  3. i >> testmsg;
  4. cout << testmsg << endl;
  5. }while(testmsg);
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: Need help reading in a file

 
0
  #3
Sep 5th, 2005
Originally Posted by Raven11
Try

  1. while(!eof)
I tried this one
  1. while (!i.eof)
  2. {blah blah}
and it tells me "invalid use of memeber (did you forget the '&' ?)'
"in argument to unary !"

I also tried this with
  1. while (!eof)
and it tells me that this is " 'eof' undeclared (first use this function)"


Originally Posted by Raven11
or
  1. do
  2. {
  3. i >> testmsg;
  4. cout << testmsg << endl;
  5. }while(testmsg);
I tried this one also and it works but with the same problem I originally described. Thanks for the response though.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need help reading in a file

 
0
  #4
Sep 5th, 2005
try

while ( i >> testmsg )
{
..........
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Need help reading in a file

 
0
  #5
Sep 5th, 2005
Dave Sinkula reports something like that and a smart solution in thread:
http://www.daniweb.com/techtalkforum...ad19956-2.html

It could be compiler dependent, since I was not able to repeat the problem.
May 'the Google' be with you!
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



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

©2003 - 2009 DaniWeb® LLC