Valhalla Legends Forums Archive | C/C++ Programming | fstream and istream help?

AuthorMessageTime
jonasr
Hello, I fairly new at C++ and I was wondering How I could get fstream to pull a string from a file then compare it with what was entered in another string!  Then if they didn't match to check the next string in the file and see if they match, and loop intill it finds a match!
June 25, 2005, 6:02 PM
Yoni
[code]std::fstream fin ( ... );
...
std::string s1, s2;
s1 = ...;
fin >> s2;

if (s1 == s2) {
  ...
}[/code]

Oh, and... Put it inside a loop.

(Hint: If you read between the lines, you will see that I am trying to get you to learn how to find the answer by yourself.)
June 25, 2005, 10:13 PM
Yegg
I suggest checking this site out, it has the standard C libraries, their functions, what they do and how to use them, and examples of how to use them.
http://www.cplusplus.com/ref/
June 25, 2005, 11:40 PM
jonasr
Thanks.
I'm not real familiar with looping so do how I do that.
I kind of know how to do it were it loops till it reaches a certin amount of times.
Then does the ... stand for the string I am comparing?
June 27, 2005, 7:12 PM
R.a.B.B.i.T
The ... means "do whatever".  Here's a little expansion on Yoni's code:
[code]std::fstream fin ( ... );
...
std::string s1, s2;
s1 = ...;
fin >> s2;
whiile(s2 != "")
{
    if (s1 == s2)
    {
        ...
        break; // ?
    }
    fin >> s2;
}[/code]
June 27, 2005, 9:13 PM
Mangix
[quote author=jonasr link=topic=11972.msg117769#msg117769 date=1119899536]
Thanks.
I'm not real familiar with looping so do how I do that.
[/quote]
you do realize that looping is the "meat" of coding right?

on another note, if you dont understand how to make a loop, then find the tutorial on that site that Yegg gave you. it explains everything you need to know.

edit:btw, you can do "using namespace std;" so that you dont have to type all that std stuff :P.
June 30, 2005, 1:09 AM
R.a.B.B.i.T
You don't have to type "std::" if you include the right header files, and namespaces [still] suck.

Includes:
#include <iostream.h>
#include <fstream.h>
June 30, 2005, 10:38 PM
K
[quote author=rabbit link=topic=11972.msg118253#msg118253 date=1120171138]
You don't have to type "std::" if you include the right header files, and namespaces [still] suck.

Includes:
#include <iostream.h>
#include <fstream.h>
[/quote]

Translation: you don't have to type std:: if you use header files which were never part of the standard and may not exist or may be deprecated.

July 1, 2005, 12:20 AM
R.a.B.B.i.T
Exactly.
July 3, 2005, 3:22 PM

Search