FILES(3pub)

✨ FILES(3pub) β€” file manipulation utilities, but make it cute ✨

okay bestie, listen up πŸ’– this is the FILES page from the C Programmer's Manual (yes, that C Programmer's Manual, the publib one, iykyk) and it's literally all about messing with files. no cap.


🌸 NAME 🌸

xgetaline, xfseek, xfopen, xfclose, getaline, fassert β€” file manipulation utilities πŸŽ€


✨ SYNOPSIS ✨

#include <files.h>

FILE *xfopen(const char *filename, const char *mode);
void xfclose(FILE *fp);
void xfseek(FILE *fp, long offset, int origin);
char *getaline(FILE *fp);
char *xgetaline(FILE *fp);
void fassert(FILE *fp);

look at that lineup. iconic. πŸ‘‘


πŸ’… DESCRIPTION πŸ’…

so these functions are, like, useful for file manipulation. that's the whole vibe. πŸ’–

here's the tea on the x girlies: the functions that begin with x work exactly like the functions without that letter β€” except if there's an error, they print an error message and kill the program. like, straight up. no chill. πŸ’€ so xfopen is fopen but with main character energy and zero tolerance.

πŸ¦‹ getaline πŸ¦‹

getaline reads a line from the given file. it allocates the memory for the line with malloc(3), and returns a pointer to the beginning of the line. if there's an error, it returns NULL.

bestie if the returned value is not NULL, the caller is responsible for freeing the memory. that's on you. don't forget or it's a leak and we don't do leaks in this house 🫢

also the newline gets removed from the end of the line. so clean. so fresh. ✨

🌈 fassert 🌈

fassert checks that the argument is not NULL, and that (for a non-NULL argument) the file does not have its error indicator flag set. if either condition is true, it prints an error message and terminates the program 😭. if neither condition is true, it does nothing. literally nothing. silent. respectful. πŸ™ˆ

so what's it for? it's for adding checks that the I/O in a program is going well. but ngl, it's mostly useful only for small programs, because more serious programs need to handle the errors more gracefully. fr fr. the little guys get fassert, the big guys gotta cope. πŸ’…


πŸ‘€ SEE ALSO πŸ‘€

go read that one too, bestie, it's the whole family 🫢


πŸŽ€ AUTHOR πŸŽ€

Lars Wirzenius (lars.wirzenius@helsinki.fi) β€” the icon, the legend, the one who wrote this πŸ’œ


πŸ’– that's the whole page! no notes! slay! πŸ’–

and that's a wrap on the Publib edition of the C Programmer's Manual page FILES(3pub) β€” shoutout to publib for the vibes, mwah, periodt βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from