entering a string into command prompt

The below code is what I'm trying to compile, but dev-c++ is sure something is wrong. Is this not the proper way to input a string (usbdriveletter) into the command prompt?

(void)system("xcopy \"" << usbdriveletter << ":FILES\\dynamic dns\\Inadyn\\*\" \"C:\\Program Files\\\" /m /h /e /v /i /q /r");
Last edited on
closed account (z05DSL3A)
Somthing like this maybe?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <iostream>


/*****************************************************************************/
int main() 
{
    using namespace std;
    string quote("\"");
    string space(" ");
    string drive("c");
    string baseCommand("copy");
    string source(":\\test.txt");
    string destination("c:\\test.old");
    string switches("/v");

    string command("");
    command += quote;
    command += baseCommand + space;
    command += quote + drive + source + quote + space;
    command += quote + destination + quote + space;
    command +=  switches;
    command += quote;

    system(command.c_str());
}


Last edited on
You've lost me. Is there not a simple way to do this in one line?
edit-I kind of see it now. All the ones in the 2nd half were just examples of how it could be laid out I see.
Last edited on
I have to go with Grey Wolf on this. You need to split it all out into its component parts to get to the bottom of your problem. Maybe you could then start putting it back together in a single line a bit at time if you really wanted to do that. I have to say if I was a developer having to modify that line I wouldn't be calling you anything nice for having written it like that.
Considering how small this program is you wouldn't mind at all.
So why squash it all up? Compilers don't get upset at white space.
closed account (z05DSL3A)
Sorry, Yes it builds a string from component parts, because you need to build a string such as "copy "c:\some path\file.txt" c:\some other path\file.txt" ", having a lot of \" and other escaped characters around can be a pain to maintain later.
Thanks wolf. That worked out great.
closed account (z05DSL3A)
You can also do somthing like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
#include <sstream>

/*****************************************************************************/
int main() 
{
	using namespace std;

	stringstream ss (stringstream::in | stringstream::out);
	char usbdriveletter= 'c';

	ss << "xcopy \"" << usbdriveletter << ":\\FILES\\dynamic dns\\Inadyn\\*\" \"C:\\Program Files\\\" /m /h /e /v /i /q /r";
	
	string command = ss.str();
	system(command.c_str());
}
Topic archived. No new replies allowed.