• Welcome to Valhalla Legends Archive.
 

fstream and istream help?

Started by jonasr, June 25, 2005, 01:02 PM

Previous topic - Next topic

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!

Yoni

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

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/

jonasr

#3
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?

R.a.B.B.i.T

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;
}

Mangix

#5
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.

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>

K

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.


R.a.B.B.i.T