0

Disclaimer: I am not looking on how to use the system() function. I didn't really know how to phrase the title.

I am trying to use pushd (not only pushd, but this is the easiest example I could find) from inside a C program like this: system("pushd ~");, but I get the following message: sh: 1: pushd: not found

I think I may need something like a pipe, but I'm not sure, and I'm not very familiar as to how this works. Sorry for the vague question, I don't really know how to explain better. Thanks for the help!

H-005
  • 467
  • 3
  • 15
  • 1
    What are you trying to do? `pushd` , isn't a program, it's just a shell instruction for manipulating the current directory (same applies for `cd`) – Alan Birtles May 27 '21 at 08:11
  • 1
    Is this what you are looking for? https://stackoverflow.com/questions/3485166/change-the-current-working-directory-in-c – ypnos May 27 '21 at 08:15
  • If it's "from inside a C program" then remove the C++ tag from your question. Otherwise have a look at https://en.cppreference.com/w/cpp/filesystem/current_path – m88 May 27 '21 at 08:19
  • @AlanBirtles My exact problem is the following: I have to run a program, and that program accesses some other files in the same directory, using a relative path. If I run the program like this: `system("/full_path/program");`, it will have problems when it tries to access the files that it needs, since it will use the relative path from where I ran the C program – H-005 May 27 '21 at 08:20
  • @m88 Should I not use the C++ tag for questions that apply to both C or C++? I felt like maybe there are people looking through the C++ tags that would know the answer, since it applies to both languages. I will remove it though – H-005 May 27 '21 at 08:22
  • [man 3 getcwd()](https://man7.org/linux/man-pages/man3/getcwd.3.html) ?? – David C. Rankin May 27 '21 at 08:24
  • @DavidC.Rankin I don't think it helps with what I want to do. From what I understand it gives me the path from where I run the C program, and I am not interested in that. – H-005 May 27 '21 at 08:34
  • @H-005 Look at the C tag description, the last sentence. – m88 May 27 '21 at 08:36
  • 1
    https://man7.org/linux/man-pages/man2/chdir.2.html, even if your `system` command worked it would only apply inside the command, it wouldn't change the path of your process – Alan Birtles May 27 '21 at 08:36
  • @m88 Understood, thank you – H-005 May 27 '21 at 08:40
  • @ypnos I think this works for what I want to do, thank you – H-005 May 27 '21 at 08:40

0 Answers0