Konrad
Konrad

Reputation: 24671

C++ windows system ("path") not working if there is space somewhere

My path to the executable file is:

C:\Users\FirstName LastName\Desktop\Saturated.exe

My program is:

while (s != "Exit")
{
    cin >> s;
    system (s.c_str());
}

Where s is string.

I tried to write:

C:\\Users\\FirstName LastName\\Desktop\\Saturated.exe

\"C:\\Users\\FirstName LastName\\Desktop\\Saturated.exe\"

C:/Users/FirstName LastName/Desktop/Saturated.exe

But none of this worked because of space between FirstName and LastName. What should I do?

Upvotes: 0

Views: 159

Answers (1)

LogicStuff
LogicStuff

Reputation: 19607

If you used the command line arguments to input the string, your OS would parse it correctly. If you want to input the path while running the program, your best chance is to go with std::getline, you'll read the whole line no matter what, no need for ".

Or, if you want to implement that same parsing behavior, you'll check if the first character is " (with cin.peek()), if that's the case, you'll cin.ignore() and std::getline until another ", otherwise you'll just cin >> s;.

Upvotes: 2

Related Questions