I have a program reading from a file "foo" using C++ using:
pFile = fopen ("foo" , "r");
I want it to stop executing the rest of the function if the file is a named pipe. Is there a way to check if the file is a named pipe before opening it?
I found the exact same question using python: Check if file is a named pipe (fifo) in python? Can I do something similar in C++?
From man 2 stat
:
int fstat(int filedes, struct stat *buf);
...The following POSIX macros are defined to check the file type using the st_mode field:
S_ISFIFO(m) FIFO (named pipe)?
So struct stat st; ... !fstat(fileno(pFile, &st) && S_ISFIFO(st.st_mode)
should work.
Edit: See also SzG's excellent answer, and Brian's comment to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With