• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 864
  • Last Modified:

Store System() call result in a string

How do I store the result of a system call?

For example: char *path = system("pwd");

I know the above is not possible. Can you tell me what i need to do to store the result of the pwd command?
0
Cool-Guy
Asked:
Cool-Guy
  • 3
1 Solution
 
Infinity08Commented:
>> Can you tell me what i need to do to store the result of the pwd command?

Don't use system. Consider using popen instead :

        http://opengroup.org/onlinepubs/007908775/xsh/popen.html

You can then read the output of the command the same way you read a file.
0
 
Infinity08Commented:
Obviously, if you only need to retrieve the working directory, you can use platform specific methods, like getcwd :

        http://linux.die.net/man/3/getcwd
0
 
Duncan RoeSoftware DeveloperCommented:
A system() call can't change the working directory of the calling process
0
 
Cool-GuyAuthor Commented:
thanks
0
 
Infinity08Commented:
May I ask why you gave a B grade ? that usually means that something was missing in the answer and/or that something is still unclear. If that's the case, then do not hesitate to ask for clarification where needed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now