mardi 21 mai 2019

What is the most efficient way to read lines from file and discard them in C or C++?

Right now I am doing this with C++ 11:

long long int linecount;
fileifstream.open( filepath );

if( std::getline( fileifstream, newline ) ) 
{
    linecount += 1;
    // do some stuff with line and throw it out
}

fileifstream.close();

I would try to use this C getline() but it is not available under Windows/Cygwin:

FILE * ifile = fopen( filename, "r" );
size_t linesz = 0;
char * line = nullptr;
unsigned int linecount = 0;

while( getline( &line, &linesz, ifile ) > 0 ) 
{
    linecount++;
    // do some stuff with line and throw it out
}

free( line );
std::cout << "Lines count: " << linecount << std::endl;
fclose(ifile);

Related:

  1. https://codereview.stackexchange.com/questions/115744/c-stack-implementation-using-stdlist
  2. std::ifstream in binary mode and locale in C++
  3. https://codereview.stackexchange.com/questions/119219/c-getline-implementation
  4. Performance difference of getline operating with istream and FILE*

Aucun commentaire:

Enregistrer un commentaire