how do I find it out?
Printable View
how do I find it out?
Code:std::ifstream fin("file.txt", std::ios::in);
int lines = 0;
char c;
do
{
fin.get(c);
if (c == '\n')
++lines;
} while (!fin.eof());
thanks!
That's not only inefficient, it's wrong. Using fin.eof() as the loop condition means you'll read one past eof, also what happens if the last line doesn't end with a newline? Then the number will be off. It's better to read whole lines instead of testing characters.Code:std::ifstream fin("file.txt", std::ios::in);
int lines = 0;
char c;
do
{
fin.get(c);
if (c == '\n')
++lines;
} while (!fin.eof());
Code:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin("test.txt");
int lines = 0;
string line;
while (getline(fin, line))
lines++;
cout << "There are " << lines << " lines" <<endl;
return 0;
}
Inefficient? I found reading a single character to be much quicker on my system.
Your way is also flawed. If the file doesn't end in a '\n', you increment lines anyway.
Here's a modified method which seems to be more efficient, and correct:Code:std::ifstream fin("file.txt", std::ios::in);
char c;
int lines = 0;
while (fin.get(c))
{
if (c == '\n')
lines++;
}
fin.close();
That's between you and your system, buffered input is almost always faster than character by character input.Quote:
I found reading a single character to be much quicker on my system.
That was my point entirely, your way printed 2 with a 3 line file that didn't end in a newline, my way printed 3, as it should.Quote:
If the file doesn't end in a '\n', you increment lines anyway.
Why would my system be an exception?Quote:
Originally posted by Crimpy
That's between you and your system, buffered input is almost always faster than character by character input.
That was my point entirely, your way printed 2 with a 3 line file that didn't end in a newline, my way printed 3, as it should.
Then I guess our definitions of lines differ.
How the hell should I know? All I know is that of several different operating systems I've used, all of them run faster with buffered input than with character by character. Or to spell it out since you don't seem to understand, one call to getline is faster than lots of calls to get.Quote:
Why would my system be an exception?
No, you're just not getting it.Quote:
Then I guess our definitions of lines differ.
I don't know, but it seems vVv does.Quote:
How the hell should I know?
To go along with your tone, how the hell would you know? :rolleyes: I get it completly, go back and read my post.Quote:
No, you're just not getting it.
Now, take a few deep breaths.
doesn't the getline() function go through every character (like the aforementioned code) looking for a '\n'? If so, then it probably would be quicker to do it yourself. of course, the getline() function might be optimized w/ different stuff (assembly perhaps?).
or, i could be wrong. if so, go easy on me. ;)
>>Then I guess our definitions of lines differ.
If a file has one sentence in, but it that doesn't end in a \n, how many lines are in the file?
Personally, I'd want the answer to be 1, as 0 just seems wrong.