0

I am developing a C terminal application that verifies if a file extension matches the file real type (ex: file with txt extension but contains PNG image). My question is: how do I use pipes in the command written in the exec function?

The following code line represents what I am trying to do: args.file_arg[i] is the filename (there will be multiple files being verified).

execlp("file", "file", (char *)args.file_arg[i], NULL)

The output returns:

EI_PA.1S2021-22.proj_checkFile---v2.pdf: PDF document, version 1.7

and I want to substitute spaces to hashtags, to then cut the desired column and show only the file "real" extension.

Can I do that without using the pipe function in C?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 2
    Does this answer your question? [How can I run an external program from C and parse its output?](https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output) – kaylum Oct 04 '21 at 10:25
  • `execlp` does not return at all (unless it fails). It certainly does not return the string you describe. If you want to capture the output of another program, you *cannot* use `exec*`. But you can `exec` a program that contains a pipeline that does the filtering for you. eg, `execlp("sh", "sh", "-c", ..., NULL)` – William Pursell Oct 04 '21 at 11:53

0 Answers0