opening ifstream with char

The program runs, but it exits when I give it an ifstream file with a char variable entered by the user. Looks like I am doing it exactly like the text. Just seems as though the path isn't working. Any suggestion?





#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

void get_file(ifstream& fin, ofstream& fout);
/* to get, open, and test the files for input and output data.*/


int main(int argc, char *argv[])
{

ifstream fin;
ofstream fout;
get_file (fin, fout);

system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n";
cin >> in_file;
fin.open(in_file);
if (fin.fail())
{
cout << "Input file failed.\n";
exit(1);
}
cout << "What is the name of the file where you would like to store data?\n";
cin >> out_file;
fout.open(out_file);
if (fout.fail())
{
cout << "imput file failed.\n";
exit(1);
}
}
Any suggestion?


Debug your code to find the problem. For example, put

cout << "File to be opened is: " << in_file << endl;
right after you read in the file to be opened.

If that looks right, check if the file to be opened exists in the same directory as the executable that is running (which, if you're using an IDE, will almost certianly NOT be the same directory that the source code is in).
Topic archived. No new replies allowed.