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

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

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.

Comments
Too right, and much better than system calls which should be avoided at all costs.

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!

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 <fstream> library for file I/O

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!

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 a const char* between its parenthesis, which is provided by the c_str() member function.

This article has been dead for over six months. Start a new discussion instead.