1. In the first part, I try to count the lines of a text file.
All things are good since here...
2. In the second part, I close the file fbin, and open it again, to make the
get pointer of the file, at the start. Then I ask you to give a number of
line, to show it in the screen .
All things are good since here TOO ...
PROBLEM:
If I change the 3 lines of code in the start of the 2nd part (close-open file), with the code inside /**/ (I tried to make the get pointer of the file, at the start), it does not work.
The get pointer seems to be stuck at the end of file... CAN YOY HELP ME ??
THANKS IN ADVANCE !!!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buf[500];
int n;
ifstream fbin("/home/tom/Desktop/a1", ios::binary);
if(!fbin) {return 1;}
// FIRST PART
int count = 0;
while(fbin)
{
fbin.getline(buf, 500);
count++;
if(fbin.peek() == EOF) break;
}
// SECOND PART
fbin.close();
fbin.open("/home/tom/Desktop/a1", ios::binary);
if(!fbin) {return 2;}
/*
fbin.seekg(0);
*/
do
{
cout << endl << "All the lines of the file are : " << count
<< endl << "Give the number of line to show "
<< "(it must be <= " << count << ") : ";
cin >> n;
if(n > count)
cout << endl << "PROBLEM ..." << endl
<< "The line you entered does not exist !"
<< endl;
}while(n > count);
#include <fstream>
#include <string>
#include <iostream>
int main()
{
// do not use ios::binary if you want to read a stream in text mode
std::ifstream file( "/home/tom/Desktop/a1" ) ;
std::streamoff lines = 0 ;
std::string line ;
while( std::getline( file, line ) ) ++lines ;
// the stream is now in a failed (hopefully eof) state
std::cout << "#lines: " << lines << '\n' ;
// before we can do anything more with the stream, the state must be cleared
file.clear() ; // clear the failed state
file.seekg(0) ; // seek to the first character in the file
// ...
}
Friend THANK YOU A LOT for your help !!!!
The information you told me are precious ....
I used something "file.clear()", before the "fbin.seekg(0);" in my code , and MAGIC .....
It works !!!
But I don't understand what "failed state", it cleared ???
Where did it found an "open" error ???
Can you help me my friend ??