Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file is a named pipe (fifo) in C++

Tags:

c++

fifo

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++?

like image 914
crypton480 Avatar asked Feb 14 '23 22:02

crypton480


1 Answers

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.

like image 142
Joseph Quinsey Avatar answered Mar 04 '23 13:03

Joseph Quinsey