View Single Post
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Line count not working....

 
0
  #3
Sep 4th, 2008
Actually, it does check whether the file is open and it does also show the contents of the file...The only thing that doesn't work is the line count I made. But I see what you mean: Below is the whole code:

  1.  
  2. unsigned int lc=0;
  3. reDo:
  4. cout << "\n\n\n\n\tUser, please Enter the filename (with directory) below:\n\n\t\t::==:: ";
  5. getline(cin, filename_user);
  6.  
  7. ifstream read(filename_user.c_str());
  8.  
  9. if(read.fail())
  10. {
  11. cout << "\n\n\t";
  12. error_slashes();
  13. cerr << "\n\n\t Sorry, but this program was unable to locate/read\n\n\t\t\tthe given file!\n\n\t";
  14. error_slashes();
  15.  
  16. cin.get();
  17. cout << "\n\n\t\tDo you wish to open a new file? (y/n[quit]) ";
  18. cin >> choice;
  19.  
  20. if(choice=='y')
  21. {
  22. goto reDo;
  23. }
  24. else
  25. {
  26. exit(1);
  27. }
  28. }
  29. read.open(filename_user.c_str(), ios::in);
  30. while(getline(read, linecount, '\n'))
  31. {
  32. ++lc;
  33. }
  34. read.clear();//clear memory
  35. read.close();
  36. read.open(filename_user.c_str(), ios::in);
  37. while(getline(read, lines, '\n'))
  38. {
  39. cout << lines << "\n\n\n";
  40. }
  41. cout << "\n\n\t\t" << lc;

In the code, it checks for the existence of the file before opening...and it does output the file contents...Only thing not working is the line count.

Is the method correct?
Last edited by amrith92; Sep 4th, 2008 at 4:36 am.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote