c++ system() function??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 36
Reputation: kodiak is an unknown quantity at this point 
Solved Threads: 0
kodiak's Avatar
kodiak kodiak is offline Offline
Light Poster

c++ system() function??

 
0
  #1
Jul 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: c++ system() function??

 
1
  #2
Jul 8th, 2007
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

  1. system("cd .. && DIR");
  2.  
  3. //or
  4.  
  5. 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/....mspx?mfr=true
Last edited by Killer_Typo; Jul 8th, 2007 at 1:25 am.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: c++ system() function??

 
1
  #3
Jul 8th, 2007
If you're trying to do
  1. system( "cd \\the\\yellow\\brick\\road" );
  2. 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
  1. chdir( "\\the\\yellow\\brick\\road" );
  2. ifstream fin( "lion.txt" );
or
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1
Reputation: SebyTheDragon is an unknown quantity at this point 
Solved Threads: 0
SebyTheDragon SebyTheDragon is offline Offline
Newbie Poster

Re: c++ system() function??

 
0
  #4
Jul 10th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: c++ system() function??

 
0
  #5
Jul 10th, 2007
Originally Posted by SebyTheDragon View Post
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 -
  1. #include <string>
  2.  
  3. /* ... */
  4.  
  5. std::string my_string("HP DC 7700.bat");
  6. system( my_string.c_str() );
  7.  
  8. /* ... */
the system() function requires a const char* between its parenthesis, which is provided by the c_str() member function.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC