Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: jonasr on June 25, 2005, 01:02 PM

Title: fstream and istream help?
Post by: jonasr on June 25, 2005, 01:02 PM
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!
Title: Re: fstream and istream help?
Post by: Yoni on June 25, 2005, 05:13 PM
std::fstream fin ( ... );
...
std::string s1, s2;
s1 = ...;
fin >> s2;

if (s1 == s2) {
  ...
}


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.)
Title: Re: fstream and istream help?
Post by: Yegg on June 25, 2005, 06:40 PM
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/
Title: Re: fstream and istream help?
Post by: jonasr on June 27, 2005, 02:12 PM
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?
Title: Re: fstream and istream help?
Post by: R.a.B.B.i.T on June 27, 2005, 04:13 PM
The ... means "do whatever".  Here's a little expansion on Yoni's code:
std::fstream fin ( ... );
...
std::string s1, s2;
s1 = ...;
fin >> s2;
whiile(s2 != "")
{
    if (s1 == s2)
    {
        ...
        break; // ?
    }
    fin >> s2;
}
Title: Re: fstream and istream help?
Post by: Mangix on June 29, 2005, 08:09 PM
Quote from: jonasr on June 27, 2005, 02:12 PM
Thanks.
I'm not real familiar with looping so do how I do that.
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.
Title: Re: fstream and istream help?
Post by: R.a.B.B.i.T on June 30, 2005, 05:38 PM
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>
Title: Re: fstream and istream help?
Post by: K on June 30, 2005, 07:20 PM
Quote from: rabbit on June 30, 2005, 05:38 PM
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>

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.

Title: Re: fstream and istream help?
Post by: R.a.B.B.i.T on July 03, 2005, 10:22 AM
Exactly.