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.

Recommended Answers

All 4 Replies

>void main(){
main returns int. In C++ main returns 0 automagically, so you don't even have the excuse of saving keystrokes anymore.

>input2 = "\"" + input + "\"";
This is unnecessary. In fact, that's likely a big part of your problem.

>while(!infile.eof()){
The timing of eof() is such that this won't work. The eofbit is only set after you try and fail to read, which means that your loop has an off-by-one error.

all right, i will take note of your advice.

anyway, the program runs smoothly if i specify the path of the file in the code. that is, replacing this

infile.open(filepath, ios::in);

with this

infile.open(C://cars.txt,ios::in);

.

i just want to know why it does not work when i get the user to specify the file path instead. As for this line,

input2 = "\"" + input + "\"";

i am trying to concantante quotation marks to the filepath that the user specified. it will then be converted to a character array and put into infile.open(...............

is there a better way to do this? which is simply open a file using the path entered by the user

>all right, i will take note of your advice.
>anyway,
Anyway, you clearly didn't bother to fully read and comprehend my previous post because my advice included a fix for your immediate problem.

>i just want to know why it does not work when
>i get the user to specify the file path instead.
Because you're doing it wrong. You're also comparing two different pieces of code as if they were the same. Here, look:

// This is what happens when you manually specify the name
// Note that I fixed your habitual syntax error by adding quotes
infile.open("C://cars.txt", ios::in);
// This is what happens when you use the string object,
// based on the exact contents of the string
infile.open("\"C://cars.txt\"", ios::in);

In the second example you're trying to open the file called "C://cars.txt", where the quotations are a part of the name. That's not the case on your file system, so the specified file does not exist. Is that clear enough for you?

And for future reference, you don't need to play games with double (back)slashes with input. The only time where a backslash makes a difference is in a string literal, where the backslash introduces an escape character:

const char *filename = "C:\file.txt"; // Wrong!

Since one backslash is an escape character, you need to use the escape character representing a backslash, which is two backslashes:

const char *filename = "C:\\file.txt"; // Correct

This doesn't apply to forward slashes though.

commented: Simple great! +3

ok thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.