Computing.Net > Forums > Programming > C Program System()

C Program System()

Reply to Message Icon

Original Message
Name: cool_andy
Date: May 14, 2007 at 03:06:40 Pacific
Subject: C Program System()
OS: Windows XP
CPU/Ram: 512
Model/Manufacturer: Acer
Comment:

Hi,
In my C Program I have system calls to execute DOS commands and Visual Sourcesafe commands.
This one works ok
system("set");

However if I try to set a variable nothing seems to happen?
//sets test varaible to 3
system("set ztest= 3");

//gets first line in textfile
system("set /P zdir= < output.txt");

This command however for Souresafe does work
system("ss properties \"$/EPCS Base Components/Miscellaneous/Menu Conversion/lib.h\" -O@version.txt");

If if I could I successfully run the SET command above to store the first line, then I would be able to run this one directly after:
system("ss properties \"%zdir%\" -O@version.txt");

I have noticed the '\' the back slashes seem to print the characters which are required, like the '"' in the command above. Is this perhaps required in SET user variables?
Maybe something like?
system("set \/P zdir= < output.txt");

Or maybe forward slash '/' because /P is a flag which is needed to get the first line?

I could be completely wrong but any help is much appreciated.

Thank you

Andrew Ashcroft


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: May 14, 2007 at 04:53:25 Pacific
Subject: C Program System()
Reply: (edit)

The problem you are facing with Set is due to the dynamic of child environments: when you issue a system("...") statement a secondary command processor is invoked to perform the required action and a new environment is built. That inherites the native one, but any change operated on it is lost at command completion. So you can't set variables this way as they are not retained from one call to the other.


Report Offensive Follow Up For Removal

Response Number 2
Name: cool_andy
Date: May 14, 2007 at 06:11:34 Pacific
Subject: C Program System()
Reply: (edit)

Ok, would it be possible to run a batch file in system()?

For example:
system("properties");
or
system("call properties.bat")

If not, I will have to find some alternative way.
Basically what I want to do is execute this sourcesafe command.
This one already works:
system("ss properties \"$/EPCS Base Components/Miscellaneous/Menu Conversion/lib.h\" -O@version.txt");

I would like to substitute the directory with:
system("ss properties "variable" -O@version.txt");

The variable will store the directory path:
$/EPCS Base Components/Miscellaneous/Menu Conversion/lib.h

I could do this using a %variable%, but it would be even better if I can use a C char variable within System()?
If I wanted to use %variable%, thats why I need to execute set /P zdir= < output.txt

Thanks again for your help

Andrew Ashcroft


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: May 14, 2007 at 06:58:04 Pacific
Subject: C Program System()
Reply: (edit)

Yes, you can run a whole batch by the System() statement and in that way you can manipulate variables in its environment.

To follow your code you could set up

system("prop output.txt")

where prop.bat accept the output.txt content by

Set /P zdir=< %1

being %1 matched with output.txt at run time.

You can use a string variable as argument of system() and set the string by C code.


Report Offensive Follow Up For Removal

Response Number 4
Name: cool_andy
Date: May 16, 2007 at 03:26:58 Pacific
Subject: C Program System()
Reply: (edit)

Ok, when I do system("prop output.txt");

It just displays the text:
off
set /P zdir=
ss properties "" -O@version.txt

prop.bat contains:
@echo off
set /P zdir=< %1
ss properties "%zdir%" -O@version.txt

I dont understand why it does not run any of the commands above?

Andrew


Report Offensive Follow Up For Removal

Response Number 5
Name: IVO
Date: May 16, 2007 at 06:06:59 Pacific
Subject: C Program System()
Reply: (edit)

Before I start a boring lesson about command processors, try rhis

system("cmd /C prop output.txt");

and report what you get.



Report Offensive Follow Up For Removal


Response Number 6
Name: cool_andy
Date: May 16, 2007 at 09:49:12 Pacific
Subject: C Program System()
Reply: (edit)

system("cmd /C prop output.txt");

This runs prop.bat successfully.
It reports the properties of the directory path given in output.txt

Example:
File: $/EPCS Base Components/pclib/lib.h
Type: Text
Size: 29418 bytes 787 lines
Store only latest version: No
Latest:
Version: 94
Date: 15/03/07 11:38
Comment:

Thank you for your help


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C Program System()

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge