I am trying to open a file after getting its path from the user. here is the code.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void main(){
const char* filepath;
string input, input2;
char lines[110];
ifstream infile;
cout << "Enter filename (including path): ";
getline (cin, input);
input2 = "\"" + input + "\"";
filepath = input2.c_str();
cout << filepath << endl;
system ("pause");
infile.open(filepath, ios::in);
if(!infile){
cerr << "Unable to open file!";
exit(1);
}
int i = 1;
while(!infile.eof()){
infile.getline(lines, 110);
i++;
}
cout << i << endl;
infile.close();
system ("pause");
}
when i run the program and i enter
C://cars.txt
, the program closes.
now the weird thigs is when i replace this
infile.open(filepath, ios::in);
with
infile.open(C://cars.txt,ios::in);
it will work.