I am trying to use the system() to open the command prompt. I want to copy all txt files in F:/AMBIENT/grid into one txt file. I am using the following command. This is opening the cmd prompt but not opening the F: or coping files into one file. I think I am doing some error in putting the syntax in the system().
the command prompt is another shell that can be used to do system commands.
system("something"); is the same thing as typing "something" into the command prompt, or putting "something" in a .bat file.
That being said, I think the command: copy *.txt new.txt is not going to work. try it in the command prompt first, but I think that new.txt will just be a copy of the last .txt file in the current folder.
I think what you want is: echo *.txt >> new.txt I think that echo will display the contents of each .txt file in your console. The > will redirect the output to new.txt. The >> will ensure that it appends the current file.
In a system command this would look like this (though I can't test it because I'm on Linux). system("echo *.txt >> new.txt");
That being said, NEVER use system. You can do the same thing with the <fstream> library. If you really wanted to be windows specific, you could use the WinAPI <windows.h>. The system command is so bad that some anti-virus software will flag your exe as a virus if it's used too often. Let's say I have a file "echo.exe" in the current directory. Well instead of actually running the intended "echo", we've just run some random executable. That's not good! We have no idea what just happened. Your code may look like it worked properly, but you may have also just uploaded your documents to some pirate's FTP.
copy does support wild cards, but you need to ensure the copy target doesn't match the wild card or you'll end up with 2 x the expected output (as the last action of type will be to copy the latest file, new.txt, into new.txt.)
copy *.txt new.foo && rename new.foo *.txt
And type is the command you need, not echo. Same deal for name wrt to the wildcard.
But, as Stewbond has already mentioned, you should be using system in the first place...
PS As echo and type are actual shell commands, sneaking an exe file with these names into a folder on the path (or your local directory) has no effect. But cmd.exe and copy.exe are another matter.
> copy does support wild cards
I thought that it was the shell that expands them, before passing it as arguments to the program
copy is a shell command, not a program, so there's no copy.exe. The copy shell command does support wild cards.
xcopy.exe is program though, and it also supports wild cards.
But the Windows command interpreter (cmd.exe, aka command prompt) isn't that helpful. And that's where system() calls end up (the system function eventually calls CreateProcess with "cmd.exe /c <your command>", after much faffing about with search paths, file extensions, etc.)
("shell" is more often used to refer to Explorer in Windows-speak, rather than the command prompt. Though both are part of the shell in the looser sense.)
Just tried your example and the arguments do expand before being send to my program.
Well, this thread is clearly about running an app on Windows using the standard command console, going by the mention of cmd.exe, etc.
So if the behaviour you are describing is when running an app in another environment then I don't see how it is relevant other than as an aside. In which case it would be have been more helpful if you had stated which environment you were using.
I did state that the behaviour I was talking about was specific to the Windows command console.
I actually had to find this thread because you had said something about it, because I'm using it right now.
I'm messing with an old program I wrote that accepts MS-style help requests (foo /?), but that was failing, and when I printed out the value of argv it was giving me the one root file with a single letter...
And I was confused, because I knew the shell did not do command-line argument expansion...
So now that I've learned how to fix it I can compile properly. :O)