Originally Posted by
kamitsuna
EDIT: Oh wait, just load all the data into variables, then use the variables you need in the code. Right?
Generally. If you have a super gigantic database or something, then you may want to deal with it differently. You can also loop thru a file like this (replace the entire part of that last program, from the comment in green, to "return 0") with:
Code:
// if program is still running, the file is ready to read
string buffer;
while (getline(file, buffer)) {
if (buffer.find("new") != string::npos)
cout << buffer << endl;
}
This just prints:
newbie
How it works:
string buffer; -- reusable string variable
while (getline(file, buffer)) { -- this is the string library getline:
getline - C++ Reference
since there is also a fstream getline (which could be used: "file.getline(buffer)", if buffer were a C-string pointer) but it works differently. This one is better for our purposes, because it uses a C++ string, and not a C-string pointer.
Notice (from that page):
Parameters
is
istream object on which the extraction operation is performed.
str
string object where the extracted content is stored.
delim
The delimiting character. The operation of extracting succesive characters is stopped when this character is read.
Return Value
The same as parameter
is.
Now, I am brand new to C++, but I understand C pretty well, and I am pretty sure this means that when "file" is over, a getline() on "file" is too. I tested this and it's true. So this loop keeps going until there are no more lines to get from the file.
if (buffer.find("new") != string::npos) -- buffer is a string object, so it includes:
find - C++ Reference
A big aside (relax...I'm sorry...bare with me): HIERARCHIES are a very very very important pattern in computer programming.
HIERARCHY
Etymology: Middle English ierarchie rank or order of holy beings, from Anglo-French jerarchie, from Medieval Latin hierarchia, from Late Greek, from Greek hierarchēs
Date: 14th century
1 : a division of angels
2 a : a ruling body of clergy organized into orders or ranks each subordinate to the one above it; especially : the bishops of a province or nation b : church government by a hierarchy
3 : a body of persons in authority
4 : the classification of a group of people according to ability or to economic, social, or professional standing; also : the group so classified
5 : a graded or ranked series <a hierarchy of values>
Now, obviously a hierarchy in programming is #5, but I wanted to include this because it is actually a complex concept, and this definition illustrates it, because it is a hierarchy!
#1: The angels. (that's nothing to do with computers...)
#2: the clergy. (nope...)
#3: people who are authorities (nope...)
#4: the "classification" of people generally (nope...)
#5: a graded or ranked series <a hierarchy of values> (this is it)
Do you see how this is a graded or ranked series (from angels, to priests, to people, to our abstract concept)? Here's another one:
Code:
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
The way find() works depends how you call it. The definition which applies to
buffer.find("new")
will most likely be the third one. Notice, we do not have to supply a second argument -- it is "by default" set to 0. Also notice: find returns a "size_t" value.
Before I go on, please remember: HIERARCHIES are a very very very important pattern in computer programming. Maybe it seems abstract to you now, but keep the idea in the back of your mind. Also: neither of those hierarchies is ranked top to bottom -- the "ranking" is applied to the list (in this case, in terms of applicable meanings). The red one completes the hierarchy.
Okay. size_t is a unsigned int type. That is, a whole number >= 0.
Return Value
The position of the first occurrence in the string of the searched content.
If the content is not found, the member value
npos is returned.
It turns out (I tested this ) npos is a string object const member, and we can use it to check and see if any content was found -- it's defined as -1, which is not an unsigned value >= 0. Guess what happens? Because size_t is unsigned, npos becomes the highest possible unsigned int (which is way bigger than any number which could be a position in our string). Hopefully I'll be able to explain why that is when I explain why num_of_beast != 54 ...but enough for now.
Hopefully the rest just makes sense.