Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jason2Jason

Lines in a file. (stdio + c++)

This topic is 5679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Whats the easiest way to count the number of lines in a text file, using c++ and stdio.h file functions. Would it be looping a fgets() and just counting, or is there a function that can count lines? Thx, -J

Share this post


Link to post
Share on other sites
Advertisement

      

unsigned int getlinecount(const char* filename)
{
FILE* fp = fopen(filename,"r");
unsigned int count = 0;
static const int buffersize = 1024;
char readbuffer[buffersize];
while(!feof(fp))
{
int readsize = fread(readbuffer,sizeof(char),buffersize-1,fp);
// eos the string so that c string routines will work on it

readbuffer[readsize] = '\0';
// find all the newlines in the buffer

char* srch = readbuffer;
while((srch = strchr(srch,'\n'))!=NULL)
{
++count;
++srch; // skip over the '\n'

}
}
fclose(fp);
return count;
}


Theoretically that should do it, but I haven't tested it yet.

[edited by - daerid on February 2, 2003 4:04:15 PM]

[edited by - daerid on February 2, 2003 4:05:33 PM]

Share this post


Link to post
Share on other sites
Slight modification :


  
#include <algorithm>

unsigned int getlinecount(const char* filename)
{
FILE* fp = fopen(filename,"r");

unsigned int lines = 0;
static const int buffersize = 1024;
char readbuffer[buffersize];

while( !feof(fp) )
{
// sizeof char is guaranteed to be 1.

// we don''t need a null character either


int readsize = fread( readbuffer, 1, buffersize, fp);

// count the occurences of ''\n'' in the buffer starting at

// ''readbuffer'' and ending just before ''readbuffer+readsize''


lines += std::count( readbuffer, readbuffer+readsize, ''\n'' )
}

fclose(fp);
return lines;
}


Or if you can use iostreams instead of stdio


  
#include <fstream>
#include <iterator>
#include <algorithm>

unsigned int getlinecount(const char* filename)
{
std::ifstream ifs( filename );

std::istreambuf_iterator<char> file_begin( ifs );
std::istreambuf_iterator<char> file_end();

return std::count( file_begin, file_end, ''\n'' );
}



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!