Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use this code for opening file from path that user give as input.

C++
string pathOfFile;
//read path of file.
std::cout << "Enter path of file: ";
 
cin >> pathOfFile; 
 
ifstream fp(pathOfFile, ios::in);
 
//check validatly of file and path of file
if (!fp)
{
	cerr << "something wrong while opening file!" << endl;	
	exit(1);
}


my program is in the : "D:\My programming code\programing code c++\1-algorithm\Huffman code".

program work correctly when I enter path like C:\test\someText.txt or any thing else but when I enter "D:\My programming code\programing code c++\1-algorithm\Huffman code\someText.txt"
program can't open file and write my error phrase "something wrong while opening file!".

I use visual studio 2013 and Vs c++11.
Posted
Updated 19-Mar-15 21:31pm
v4
Comments
phil.o 19-Mar-15 8:03am    
While debugging, what is the value stored in pathOfFile variable?
farhad bat 19-Mar-15 8:12am    
Nothing
Sergey Alexandrovich Kryukov 19-Mar-15 8:35am    
Then how it could possibly work?!
—SA
farhad bat 19-Mar-15 9:01am    
I read wrong your last massage, when compiling it is : "D:\My " but I enter "D:\My programming code\programing code c++\1-algorithm\Huffman code\Huffman code\someText.txt".

i should use getline Instead of cin.
Sergey Alexandrovich Kryukov 19-Mar-15 11:02am    
Sure...
"Cin is notorious at causing input issues...". Please see:
http://www.cplusplus.com/forum/articles/6046.
I would always prefer using gets, for example, or, indeed, getline.

By why did you formally accept wrong answers?

—SA

//cin is wrong instead should use getline(cin, pathOfFile) . cin when read space halt reading more character but getline read full path

C++
string pathOfFile;
//read path of file.
std::cout << "Enter path of file: ";

//cin is wrong instead should use getline(cin, pathOfFile) 
//cin >> pathOfFile;

 getline(cin, pathOfFile);

ifstream fp(pathOfFile, ios::in);
 
//check validatly of file and path of file
if (!fp)
{
	cerr << "something wrong while opening file!" << endl;	
	exit(1);
}
 
Share this answer
 
You need to surround the pathOfFile variable with double-quotes so that the OS can handle the spaces in the path name.

E.g.
C++
pathOfFile = '\"' + pathOfFile + '\"';
 
Share this answer
 
v3
Comments
farhad bat 19-Mar-15 8:18am    
what means "surround the pathOfFile variable with double-quotes" ?
CHill60 19-Mar-15 8:34am    
See my amended solution
hi,
In Windows operating system this problem is caused because of spaces in the directory path name.
C:\test\someText.txt does not have any spaces in it but
D:\My programming code\programing code c++\1-algorithm\Huffman code has lot of spaces like between My and programming.


You can either put the path in double quotes like suggested in solution 1
OR
go for the Short path of the directory[^]. This API will get you the short path of the directory which will look like
"D:\My ~1"

On linux i am not sure if such APIs are available so please Google for it.

hope this helps !!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900