0

I created a new directory using mkdir in my program and I am trying to create a text file in it, usually if I was in the terminal I would do something like:

cd whatever/directory/I want 
touch filename.txt OR cat > filename.txt

How can I implement this in exec or some of its variants as I could only make it execute the second part without the cd part which means my file will be created but not in the directory I want.

Sergio
  • 275
  • 1
  • 15
  • 1
    You don't need to `exec` some other application to create files, you can just use `open` or `fopen` for that (both of which accept the full path of the file, so you don't actually need to 'cd' into that directory first (assuming it already exists). That being said, glibc does have the `chdir` function, which can be used to change your working directory – Felix G Apr 06 '20 at 08:56
  • @FelixG I am aware of that but my goal is to do it using `exec` and not default file stuff – Sergio Apr 06 '20 at 09:03
  • @ayrebelcoding hope it helps: https://stackoverflow.com/questions/19209141/how-do-i-execute-a-shell-built-in-command-with-a-c-function – Alberto Merciai Apr 06 '20 at 10:24

0 Answers0