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?
hess586Asked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
>>Any suggestions?

1st of all, change

     inFile.open("c:\file.txt",ios::in);

to

     inFile.open("c:\\file.txt",ios::in);

inFile is not being opened correctly, thus the reads will fail.
0
 
AxterCommented:
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!
0
 
AxterCommented:
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";
0
 
AxterCommented:
hess586,

Isn't that the same answer I posted????
Which I posted first........
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.