How can I assign “pwd” (or any other command in that case) result (present working dir) to a variable which is char*?
command can be anything. Not bounded to just “pwd”.
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Start with
popen. That will let you run a command with its standard output directed to aFILE *that your parent can read. From there it’s just a matter of reading its output like you would any normal file (e.g., withfgets,getchar, etc.)Generally, however, you’d prefer to avoid running an external program for that — you should have
getcwdavailable, which will give the same result much more directly.