~/ learn/ comp-325/ cards/ Asking a file about itself
1 of 10

Filling a struct stat, and checking that you did

Filling a struct stat, and checking that you did

Answer

struct stat sb; if (stat(path, &sb) == -1) { perror("stat"); return 1; } printf("size %lld bytes, %llu links\n", (long long)sb.st_size, (unsigned long long)sb.st_nlink);

stat returns 0 or -1, never the data — the structure is filled through the pointer you pass. The casts exist because off_t and nlink_t have no portable printf conversion.

S&K 3e ch18 §18.9; stat/lstat/fstat signatures and <sys/stat.h> macros per POSIX.1-2024

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/b5e360a1-18ef-4404-821a-fc70fd14a644/flashcard utf-8 LF