Domanda

Dato un FILE *, è possibile determinare il tipo sottostante? Cioè, c'è una funzione che mi dirà se il file * è un tubo o un socket o un file regolare su disco?

È stato utile?

Soluzione

C'è una funzione fstat(2).

NOME        stat, fstat, lstat - ottenere lo stato file

SINOSSI

   #include <sys/types.h>
   #include <sys/stat.h>
   #include <unistd.h>

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

È possibile ottenere il fd chiamando fileno(3).

Poi si può chiamare S_ISFIFO(buf) per capirlo.

Altri suggerimenti

Utilizzare la funzione fstat (). Tuttavia, è necessario utilizzare la macro fileno () per ottenere il descrittore di file da file FILE struct.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

FILE *fp = fopen(path, "r");
int fd = fileno(fp);
struct stat statbuf;

fstat(fd, &statbuf);

/* a decoding case statement would be good here */
printf("%s is file type %08o\n", path, (statbuf.st_mode & 0777000);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top