spaces file names c++

I am aware that this question has been asked over and over again, but I just can get it to work, I wonder who was the genius that came up with the idea to have spaces in file names.

system("copy C:\\Users\\test\\Desktop\\myProgram.exe C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");

This was the simplest was to make my program run on start up I know it is not the most efficient one but than again I am not an expert.

If someone could please help me out with this, I value all suggestions.
I have attempted several combinations with double quote but failed.
If you want to include an actual " (double quote ) character within a string, you need to precede it with a backslash thus, \"

See the part about Escape code on this page:
http://www.cplusplus.com/doc/tutorial/constants/
I know but I keep spinning on it. This is what I tried:

system("copy \"C:\\Users\\test\\Desktop\\myProgram.exe\" \"C:\\ProgramData\\Microsoft\\Windows\\\"Start Menu\"\\Programs\\Startup\"");

system("copy \"C:\\Users\\test\\Desktop\\myProgram.exe\" \"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\"")
The second of those looks reasonable to me, assuming you have all the actual file/path names correct.

Why do you even use system?

1
2
3
4
5
6
7
8
int main(int argc, char** argv)
{
    std::ifstream src(argv[0], std::ios::binary);
    std::ofstream dst("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Startup/myprogram.exe", std::ios::binary);
    dst << src.rdbuf();
    dst.close();
    src.close();
}


This allows for error checking, too.
Last edited on
Thank you EssGetEich learned something new today, this works fine but needs admin privileges so just run as administrator. And thank yu all for your time an effort.
It may need admin permissions since on Windows 7 some folders are protected.
You can check if a folder is protected by entering it as a normal user and trying to create a file.
If you can simply right-click and create a file, it should be good.
If you can only create a folder, it will require admin permissions.
Last edited on
Topic archived. No new replies allowed.