4

I am currently working on getting my program to execute a program (such as power point) and then beside it the path to the file I want to open. My program is getting the file's path by using:

dirIter2->path()

I get the 2 paths of the program and file, Merge them as one string and pass them into the following:

system(PathTotal.c_str())

this is working great but my only issue is that when the file name has a space in its name command prompt says it cannont find the file (becuase it thinks the file name ends when it gets to the first space. I have tried to wrap it with quotes but it is the acutal file name that need to be wrapped. (eg. i have tried "C:\users\bob\john is cool" but it needs to be like this: C:\users\bob\"john is cool")

Does anyone have any suggestions on how I could fix this? I was thinking about getting the path to the folder to where the file and then getting the file name. I would wrap the file name with quotes then add it to the folder's path. I have tried using the ->path() like above but the only problem is that it only goes to outside of the folder's directory?

Is there a boost command that could get the enitre path to the file without getting the file aswell?

I am not commited to this idea if anyone has any better suggestions

Thanks

Johnston
  • 2,873
  • 8
  • 29
  • 39
  • Invalid assumption -- it works nicely quoting the whole thing. No need to go to extremes to quote the smallest possible part. Cheers & hth., – Cheers and hth. - Alf Nov 14 '10 at 23:33
  • @Alf -- I have tried quoting the entire string -- it does not work for some reason, I had the same problem with the program path until i just put quotes where there was spaces. Thanks for the comment – Johnston Nov 15 '10 at 01:57
  • 1
    @Jonston: I suspect that you Did Something Wrong. Like, quoting "the entire string" including arguments. Or, not understanding about `\"` in C++. Or something like that. Try to reproduce the problem in an absolutely minimal but complete program. Post the code if the process of creating that program doesn't solve the problem for you. If it does solve the problem, please tell. :-) – Cheers and hth. - Alf Nov 15 '10 at 02:35

3 Answers3

1

In both C and C++, the '\' is an escape character. For certain things (like '\n' or '\t') it inserts a control code; otherwise, it just gives you the next character.

So if you do something like:

fopen("C:\users\bob\john is cool", "r");

it's going to try to open a file named

C:usersbobjohn is cool

If you want those '\' characters in the output, you have to escape them. So you'd want:

fopen("C:\\users\\bob\\john is cool", "r");

On Windows with Visual Studio, I've also successfully used Unix-style separators:

fopen("C:/users/bob/john is cool", "r");

And in fact, you can mix them up:

fopen("C:/users\\bob/john is cool", "r");
Bob Murphy
  • 5,814
  • 2
  • 32
  • 35
0

I'm not familiar with C string operations, but couldn't you do the following rather easily?

int i = path.lastIndexOf("\\"); //Find the index of the last "\"
String quotedPath = path.substring(0, i+1); //Get the path up until the last "\"
quotedPath += "\"" + path.substring(i+2) + "\""; //Add quotes and concatenate the filename

Sorry for the Java, its the closest thing that I'm familiar with. I've made this a community wiki in case someone can edit the code to the equivalent C.

JohnS
  • 1,192
  • 1
  • 11
  • 22
  • I'm not going to attempt this, as I can't make sense of it. There's too many mistaken assumptions I cna spot. For instance, the path separator is \, not / (see example "C:\users\..."). However, the path separator often occurs in arguments too: `"C:\Program Files\MyApp\MyApp.exe" "C:\Documents and Settings\Bob\My Documents\January.MyApp"` - note that you might need to quote both program name and argument. – MSalters Nov 15 '10 at 10:43
  • He didn't ask about quoting the program path, just the name. If he wanted to quote the path as well its a trivial addition to what I wrote here. But yes, I screwed up the path separator, I'll fix that now. – JohnS Nov 16 '10 at 02:26
0

I'd also like to add that sometimes it is necessary to escape spaces as in the following:

cmd.exe -C C:/Program\ Files/Application\ Folder/Executable\ with\ spaces.exe

or

cmd.exe -C C:\\Program\ Files\\Application\ Folder\\Executable\ with\ spaces.exe

g19fanatic
  • 10,567
  • 6
  • 33
  • 63