Re: Explain the magic? Counting lines in a file
From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 12/15/04
- Next message: EventHelix.com: "Re: What is a good way to define CONSTANT?"
- Previous message: Ivan Vecerina: "Re: int or bool?"
- In reply to: Dale Atkin: "Explain the magic? Counting lines in a file"
- Next in thread: Dale Atkin: "Re: Explain the magic? Counting lines in a file"
- Reply: Dale Atkin: "Re: Explain the magic? Counting lines in a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: EventHelix.com: "Re: What is a good way to define CONSTANT?"
- Previous message: Ivan Vecerina: "Re: int or bool?"
- In reply to: Dale Atkin: "Explain the magic? Counting lines in a file"
- Next in thread: Dale Atkin: "Re: Explain the magic? Counting lines in a file"
- Reply: Dale Atkin: "Re: Explain the magic? Counting lines in a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|