Link to home
Start Free TrialLog in
Avatar of hess586
hess586

asked on

Reading Strings into variables from a text file in C++

I would like to be able to read in strings into two variables and also an into into a third I'd like to do this from a text file.  This is what I have so far.

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
      string name = " ";
      string stadium = "";
      int wins = 0;
      ifstream inFile;
      inFile.open("c:\file.txt",ios::in);

      //BinarySearchTree<BaseballTeam> BaseballTree;
                inFile>> name;
      inFile>> stadium;
      inFile>> wins;
                cout<<wins<<endl;
      cout<<name<<endl;
      cout<< name<<endl;

      return 0;
}

I use the cout statements to see if anything was actually read into the variables.
Here is the sample text file .

St.LouisCardinals
BuschStadium
14
ChicagoCubs
WrigleyField
9
When i run this, the variables keep there default values and nothing is read into them.   Any suggestions?
Avatar of Axter
Axter
Flag of United States of America image

Hi hess586,
> >inFile.open("c:\file.txt",ios::in);
You need to use two \\ for every one \
inFile.open("c:\\file.txt",ios::in);

David Maisonave :-)
Cheers!
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The \ character is a special character, and if you want to represent a back slash character in a C/C++ string, you need to use two of them for each one.

char FileName[] = "c:\\windows\\system\\foofoo.exe";
hess586,

Isn't that the same answer I posted????
Which I posted first........