Accept user input to open file
I'm trying to accept user input to open up a .dat file that's in my source files but I don't know why the file keeps failing to open
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <string>
#include <fstream>
#include "arrayFunctions.h"
using namespace std;
int main()
{
string fileName;
int size = 0;
ifstream inputFile;
do
{
cout << "Please enter file name: ";
getline(cin,fileName);
inputFile.open(fileName.c_str());
if (!inputFile)
{
cout << "The file \"" << fileName << "\" failed to open.\n"
<< "Check to see if the file exists and please try again"
<< endl << endl;
}
while (inputFile.good())
{
string stop = " ";
string num;
getline(inputFile, stop);
size++;
}
} while (!inputFile);
cout << size << endl << endl;
inputFile.close();
system("pause");
}
|
Try this to see a better error msg.
1 2 3 4 5
|
#include <stdio.h>
if (!inputFile)
{
perror("File error");
}
|
Topic archived. No new replies allowed.