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<Basebal lTeam> 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?
#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",
//BinarySearchTree<Basebal
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\\foof oo.exe";
char FileName[] = "c:\\windows\\system\\foof
hess586,
Isn't that the same answer I posted????
Which I posted first........
Isn't that the same answer I posted????
Which I posted first........
> >inFile.open("c:\file.txt"
You need to use two \\ for every one \
inFile.open("c:\\file.txt"
David Maisonave :-)
Cheers!