Solved

Store System() call result in a string

Posted on 2008-10-31
5
854 Views
Last Modified: 2012-08-14
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
Comment
Question by:Cool-Guy
  • 3
5 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22848995
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22849001
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
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 22872187
A system() call can't change the working directory of the calling process
0
 

Author Closing Comment

by:Cool-Guy
ID: 31511997
thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22896141
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question