File won't open?

Sorry to post again so soon, but I'm having a different problem. I keep trying to open a .dat file with my program. The console accepts my input, but then does nothing. I can't seem to figure out the problem. Help?

//This program will take input from a user file, read the input,
//and output the first word after the first three commas
//************************************************************

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
ifstream inFile; //begin conversion function c_str
ofstream outFile;
string fileName, //allow user name file
firstWord, //get word after first comma
secondWord, //get word after second comma
thirdWord; //get word after third comma

cout << "Input file name:" << endl; //user prompt
cin >> fileName;
inFile.open(fileName.c_str()); //open file convert c_str

if(!inFile)
{
cout<<"Error opening file, the program will close." << endl << endl;
system("pause");
return 0;
}

cin.ignore(200, ','); //get words
cin >> firstWord;
cin.ignore(200, ',');
cin >> secondWord;
cin.ignore(200, ',');
cin >> thirdWord;

cout << firstWord << endl //output
<< secondWord << endl
<< thirdWord << endl;

inFile.close();

char exit_char; // temporary for input
cout << "\nPress any key and <enter> to exit \n";
cin >> exit_char; // Wait for key response before exiting

return 0;
}
Are we attempting to read the words from a file?

If we are I would expect:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int wordCount = 0;
string word;
string fileName;

cout << "Input file name:" << endl; //user prompt
cin >> fileName;
inFile.open(fileName.c_str());	 //open file convert c_str

if(inFile)
{
     while(!inFile.eof()) // a check for we are not at the end of file.
     {
           inFile >> word; // we are reading from a file....
           wordCount++;

           cout << "Word Number "<< wordCount << ": " << word << endl;
     }
     inFile.close();
}
else
{
       cout << "We had a problem with the file!!!" << endl;
}


If you note the code difference between what you did and mine you will see you used the wrong file stream. cout/cin are the console streams and ifstream/ofstream are the file streams, which you wanted to read from.

for your problem we might have to refine this a little better, but I don't know what to expect from your data file.
Last edited on
Yes, my assignment is to read the first word after three different commas from a user named file (thus the c_str). So, you're saying I need to use inFile >> word; to get the words? In that case, do I use inFile.ignore(200, ',') to get the word after the comma?

Thanks
Topic archived. No new replies allowed.