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

I have a reader from the file "foo" using C ++ using:

pFile = fopen ("foo" , "r");

I want it to stop the rest of the function if the file is a named pipe. Is there a way to check if a file is a named pipe before opening it?

I found the same question using python: Check if file is a named pipe (fifo) in python? Can I do something like this in C ++?

+4
source share
2 answers

From man 2 stat:

int fstat(int filedes, struct stat *buf);

... The following POSIX macros are defined for checking the file type using the st_mode field:

         S_ISFIFO(m) FIFO (named pipe)?

struct stat st;... !fstat(fileno(pFile, &st) && S_ISFIFO(st.st_mode) struct stat st;... !fstat(fileno(pFile, &st) && S_ISFIFO(st.st_mode) .

: . SzG .

+6

, AFTER fopen(). , syscall open() , - FIFO . stat() syscall ( Unix/Linux) fopen(), .

+1

Source: https://habr.com/ru/post/1524613/


All Articles