D
Dale Atkin
As part of a larger project, I need to be able to count the number of lines
in a file (so I know what to expect). Anyways, I came accross the following
code that seems to do the trick, the only thing is, I'm not 100% sure what
it is doing, or how.
#include<iostream>
#include<fstream>
main(int argc, char *argv[])
{
using namespace std;
ifstream fin(argv[1]);
int
numlines=count(istreambuf_iterator<char>(fin),istreambuf_iterator<char>(),
'\n');
cout << numlines;
}
I think I've managed to figure most of it out, but I'm not really sure.
First off, what does "using namespace std;" do? Please don't say it puts you
in to the standard name space, what I want to know is what namespace was I
in, and why do I want to be in the std namespace.
From what I can tell, the count function takes in a start position and an
end position (is this an address in memory or what does it take here), and a
delimeter, so would that make istreambuf_iterator<char>(fin) the begining
and istreambuf_interator<char>(), the end?
Does this make sense. The documentation I can find says that it is an
"Iterator that reads successive characters from the stream buffer for which
it was constructed", but what exactly is an iterator? How would one
generally use one? (sounds like it is something I should know about).
Is char the amount we're iterating by each time? would
istreambuf_iterator<double>(fin) make sense for a binary file full of
doubles (I don't happen to have one to test the behavior with)...
Also, a point on style, does one typically include the ".h" in the include
statement or not? I have been tending to, but most of the example code I
find doesn't have it (I know I don't need to, but will other people point at
my code and laugh at me if I do...)
Thanx,
Dale
in a file (so I know what to expect). Anyways, I came accross the following
code that seems to do the trick, the only thing is, I'm not 100% sure what
it is doing, or how.
#include<iostream>
#include<fstream>
main(int argc, char *argv[])
{
using namespace std;
ifstream fin(argv[1]);
int
numlines=count(istreambuf_iterator<char>(fin),istreambuf_iterator<char>(),
'\n');
cout << numlines;
}
I think I've managed to figure most of it out, but I'm not really sure.
First off, what does "using namespace std;" do? Please don't say it puts you
in to the standard name space, what I want to know is what namespace was I
in, and why do I want to be in the std namespace.
From what I can tell, the count function takes in a start position and an
end position (is this an address in memory or what does it take here), and a
delimeter, so would that make istreambuf_iterator<char>(fin) the begining
and istreambuf_interator<char>(), the end?
Does this make sense. The documentation I can find says that it is an
"Iterator that reads successive characters from the stream buffer for which
it was constructed", but what exactly is an iterator? How would one
generally use one? (sounds like it is something I should know about).
Is char the amount we're iterating by each time? would
istreambuf_iterator<double>(fin) make sense for a binary file full of
doubles (I don't happen to have one to test the behavior with)...
Also, a point on style, does one typically include the ".h" in the include
statement or not? I have been tending to, but most of the example code I
find doesn't have it (I know I don't need to, but will other people point at
my code and laugh at me if I do...)
Thanx,
Dale