954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ system() function??

hi, i need help with the system() function.

I need to open up a command line instance, change directory to a certain file, and open a file there. Problem is, every time call the system() function, it calls up a new command line instance. I need to do as i said above, all in a single instance, but cannot figure out how...


Thanks!!!

--Kodiak

kodiak
Light Poster
38 posts since Jul 2007
Reputation Points: 15
Solved Threads: 0
 

easy!!


this is more of a command shell question though :-/

seperate each command with the & symbol

use a && to ensure that the following command is only run if the previous was successful

so to go up a directory and get the contents

system("cd .. && DIR");
 
//or
 
system("cd .. & DIR");


the only difference is that the first version only executes if the initial cd .. executed correctly else it will not continue the second version executes each command no matter what.


BTW a good read http://207.46.196.114/windowsserver/en/library/44500063-fdaf-4e4f-8dac-476c497a166f1033.mspx?mfr=true

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

If you're trying to do

system( "cd \\the\\yellow\\brick\\road" );
ifstream fin( "lion.txt" );


then you're going about it all wrong. Any changes you make inside system() are localised to the sub-process which is created (and then lost almost immediately). The current directory seen by ifstream isn't at the end of the yellow brick road.

If you really want to open a file in another directory, then you need something like

chdir( "\\the\\yellow\\brick\\road" );
ifstream fin( "lion.txt" );


or

ifstream fin( "\\the\\yellow\\brick\\road\\lion.txt" );


But note that chdir() is a concept here, you need to find the actual name of the function which matches your OS/Compiler.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Hi Guys,

I'm having the folowing problem related to the system function.
I want to be able to read from a txt file a string and then launch the string as a bat file. For example, the text file contains "HP DC 7700" and i want to run the bat "HP DC 7700.bat".
I have no ideea on how to pass what I read from the txt to the system function....
Can you help me out?

Thanks and cheers!

SebyTheDragon
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Hi Guys,

I'm having the folowing problem related to the system function. I want to be able to read from a txt file a string

This part doesn't have anything to do with the system() function - you should look up the library for file I/Oand then launch the string as a bat file. For example, the text file contains "HP DC 7700" and i want to run the bat "HP DC 7700.bat".
I have no ideea on how to pass what I read from the txt to the system function....
Can you help me out?

Thanks and cheers! Once you've obtained the string which you wish to execute, you can pass it as a parameter to the system function.

with a C++ string, it would look something like this -

#include <string>

/* ... */

    std::string my_string("HP DC 7700.bat");
    system( my_string.c_str() );

/* ... */


the system() function requires aconst char* between its parenthesis, which is provided by the c_str() member function.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You