"Edo" <***@eddododod.dod> wrote in message news:cl5hgp$967$***@news-02.connect.com.au...
| Karl Heinz Buchegger wrote:
| > Edo wrote:
| >
| >>Anand Hariharan wrote:
| >>
| >>>"Chris \( Val \)" <***@bigpond.com.au> wrote in message news:<***@uni-berlin.de>...
| >>>
| >>>
| >>>>"Alwyn" <***@mac.com.invalid> wrote in message
| >>>>news:191020041223380836%***@mac.com.invalid...
| >>>>| In article <cl2sit$3ta$***@news-02.connect.com.au>, Edo
| >>>>| <***@eddododod.dod> wrote:
| >>>>| >
| >>>>| > I should rewrite this... I understand what the system rutine takes, but
| >>>>| > how can I pass variables as filenames here.
| >>>>|
| >>>
| >>>
| >>>[bunches of good advice pertinent to OP's question snipped]
| >>>
| >>>To the OP -
| >>>Why not simply write code that counts the number of lines in 'infile'?
| >>> wc may be faster, but having to execute that command, and open the
| >>>outfile to read the result, and parse it for the number all is going
| >>>to offset the speed-gains.
| >>>
| >>>my $0.02,
| >>>- Anand
| >>
| >>sure, I tried, it worked only if the file size is with in a limit, but
| >>say you have a file with 2000000 lines or 400Mb the loop to count would
| >>take a loooooooong time.
| >
| >
| > Show your code. You must have done something terribly wrong.
| #include <fstream>
| using std::ifstream;
|
| #include <iostream>
| using std::cout;
| using std::endl;
#include <string>
using std::string;
using std::getline;
| int main() {
| ifstream is(filename.c_str());
if( !is )
return EXIT_FAILURE; // <cstdlib>
| string s;
| int count = 0;
| while(getline(is, s)) {
| ++count;
| }
if( is.eof() )
| cout << count << endl;
else
// Handle failed read...
| }
Btw, there is no need to actually read data into an
buffer, if you only need to count the number of lines:
You will need to include <limits>, then try the following:
std::size_t NrLines( 0 );
while( InFile.ignore( std::numeric_limits<
std::streamsize>::max(), '\n' ) )
{
++NrLines;
}
Cheers.
Chris Val