Re: Explain the magic? Counting lines in a file

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 12/15/04


Date: Wed, 15 Dec 2004 12:15:58 +0100

Dale Atkin wrote:

> 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[])

Your main() function is missing a return type.

> {
> 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.

You were (and still are) in the global namespace. "using namespace std;"
just means that the names from that namespace are now also available in the
current namespace, so e.g. instead of the need to fully qualify
std::ifstream, you can now just write ifstream, because that name was made
available in the global 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),

No, it's an iterator. Iterator types are special classes that usually belong
to container classes. Iterators are similar to pointers in usage, but they
point to a specific element of a container instead of to a specific memory
address. You can use them to iterate (hence the name) over a container by
incrementing them, just like you could use pointers to iterate over an
array by incrementing them.
Such iterators are used in most standard algorithms, so that you can use
them independant of the container type.
There are also iterators for streams, so you can use some of the standard
algorithms with them, too. std::count is one of those algorithms.

> and a delimeter, so would that make istreambuf_iterator<char>(fin) the
> begining and istreambuf_interator<char>(), the end?

Yes.

> 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).

See above.

> Is char the amount we're iterating by each time?

Yes.

> 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)...

Yes.

> 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...)

It depends. You always have to provide the full name of the header, and in
the case of the C++ standard headers, those don't end in ".h". However,
most other headers do.



Relevant Pages

  • Re: Explain the magic? Counting lines in a file
    ... It causes the namespace 'std' to be included in name lookup. ... Those iterators designate 'beginning of stream' and 'end of stream' ... but what exactly is an iterator? ... However, none of the standard headers, such as ...
    (comp.lang.cpp)
  • Re: :: with object
    ... it does not solve the problem of iterator ... > new namespace for each different iterator expression I write. ... What is wrong with typedefs? ... typedef mapmymap; ...
    (comp.lang.cpp)
  • Re: :: with object
    ... Martijn Lievaart wrote: ... >>expressions being too long in general. ... >>new namespace for each different iterator expression I write. ...
    (comp.lang.cpp)
  • Re: :: with object
    ... it does not solve the problem of iterator ... expressions being too long in general. ... new namespace for each different iterator expression I write. ... > int main ...
    (comp.lang.cpp)
  • Call for suggestions: Declaring data entry forms using Python classes
    ... For the purposes of GUI description, ... the renderer has to iterate over its fields. ... # instantiated as objects inside the container classes ... # I'm trying several versions of the iterator interface, ...
    (comp.lang.python)