Jump to content
  • Advertisement
Sign in to follow this  
andyb716

Getting line count in file C++

This topic is 5081 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

is there a fast way to get a line count in a text file. this is how im doing it. void PrintRecordCount(){ std::ifstream file("data.txt"); int RC = 0; std::string line; while(std::getline(file,line)){ RC++; } std::cout << "RecordCount: " << RC << std::endl; file.close(); } there are 700,000 lines in the file and it takes about a minute or two to comeback with the recordcount. Am I an idiot or something, can it be done faster?

Share this post


Link to post
Share on other sites
Advertisement
It looks like that code is reading in all the characters in the file. Maybe you could just do a search for all the newline characters and count them? I'm not sure how fast this would be, however.

- Mike

Share this post


Link to post
Share on other sites
Code extract from wc.c:

while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
{
register char *p = buf;

while ((p = memchr (p, '\n', (buf + bytes_read) - p)))
{
++p;
++lines;
}
bytes += bytes_read;
}




You can find this file here:
TextUtils unix source package(Scroll down for the source code download link)

- Mike

Share this post


Link to post
Share on other sites
Alterantely, you might try using streambuf iterators and see if that works fast enough for you. For example:

std::ifstream ifs("data.txt");
std::istreambuf_iterator<char> begin(ifs);
std::istreambuf_iterator<char> end;
int record_count = std::count(begin, end, '\n');

std::cout << "RecordCount: " << record_count << std::endl;

Share this post


Link to post
Share on other sites
I downloaded the source code and opened wc.c, I'm a beginner at this and I can't seem to find where they open the file there searching through. They have functions I never even heard of. Are these windows functions?

SET_BINARY (fd);

while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
{
register char *p = buf;

while ((p = memchr (p, '\n', (buf + bytes_read) - p)))
{
++p;
++lines;
}
bytes += bytes_read;
}

fd is an int. how is that a file, and how are they putting that file into the buf variable? any kind of help would be thanked. thanks for all the support.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:

register char *p = buf;

Man. How old must that code be?


Yeah, I started crying when I saw that.

Share this post


Link to post
Share on other sites
std::ifstream ifs("data.txt");
std::istreambuf_iterator<char> begin(ifs);
std::istreambuf_iterator<char> end;
int record_count = std::count(begin, end, '\n');

std::cout << "RecordCount: " << record_count << std::endl;

this only gets one less than how many lines there are because the last line doesn't have an \n new line character. it takes the same amount of time using the getline and counting it that way, plus its a little more confusing.

Share this post


Link to post
Share on other sites
You could do worse than something like SLOCCount, which is designed for code and has all sorts of funky statistics. It can do stuff like ignoring whitespace and comments :)

One of the statistics is Cost to Develop - it includes all your maintenance which is why it's so shockingly high :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!