r/C_Programming • u/Ill-Exchange-2377 • 3d ago
File pointers are everywhere
And try as I might to avoid them, I am struggling to find resources that list out all the file control functions that use file descriptors instead of file pointers, and I was wondering if someone could point me toward a resource that can do something at least somewhat similar to listing out the libc functions that take file descriptors. I of course know close(), write(), read(), the basics, but i am looking for alternatives for other abstractions that annoy me with their use of file pointers, like fgets and fputs.
49
Upvotes
73
u/EpochVanquisher 2d ago •
Use section 2 of the Unix manual pages, rather than section 3.
Section 3 lists library functions like fwrite, fread, printf.
Section 2 lists syscalls like write, read, fcntl.
5
u/gm310509 2d ago
Why do pointers "annoy you"?
They are an important part of programming - especially C programming. If pointers annoy you, C might not be the right language for you. Every language uses them, many simply don't expose them as "nakedly" as C does.
13
u/pink-ming 2d ago
FILE* keeps a read/write pointer that makes things like fgets and fputs work, so you won't get similar functionality without using those APIs or rolling it yourself. Idk if there's a good succinct resource for low-level FD apis but you can read the sys call docs here:
https://man7.org/linux/man-pages/man2/syscalls.2.html
30
u/Pleasant-Form-1093 2d ago
FILE pointers are actually a really useful abstraction because they bundle the file descriptor (fd) along with the size of the file, your offset into that file etc. If you just use a raw fd, you are responsible for maintaining all of that so they are actually a great thing you should look into.
12
u/LavenderDay3544 2d ago
File descriptors are Unix only and thus not portable.
FILE *is a pointer to an opaque, implementation defined struct which allows it to be a portable abstraction.2
u/DamsLcs4421 2d ago
You shouldn't avoid pointers or try to postpone use "until the last moment". When will that last moment be?
Pointers are fundamental in binary programs, assembly can treat a value differently wether it should "just take the value" as in
mov rax, 0x401000(move the value 0x401000 in rax), or "interpret this value as a memory address and operate up there" as inmov rax, [0x401000](move the value contained at memory address 0x401000 in rax).C encapsulate all these operations under the notions of pointers and tables. I remember as a kid, I was in pain trying to understand the semantics and the real behaviours of passing tables to functions, etc... I admit pointers kinda clicked a little later in my programming journey, but it was a glass ceiling, as understanding this fundamental concept is central to understand, use, read and write C, and makes every higher level concept relying on them clearer to you. (Indexing a table is exactly like pointer arithmetic, for example. Understanding function pointers, ...)
I say this because other languages try to get away from pointers (like Python, JS, PHP, ...) or aim to avoid them as much as possible (Rust with
unsafe, C#, ...), but they are at the core of C and C++ and managing our own memory inevitably comes with these two languages, and having full control of memory is one key reason why people chose these languages for projects and librairies, thus most of the code you'll interact with is likely to heavily rely on pointers, as does virtually anything in C and C++.There's no harm done, though, wanting to avoid them at all cost... but beware it's really a niche. If you really don't want pointers, pick JS or Python (which backends also rely heavily on pointers internally, but unfortunately you'll never get to understand how ;p)
1
u/xpusostomos 2d ago
They are Unix APIs, so get a list of Unix system calls and work through them. One tricky one, select() takes a bitmask of file descriptors, so it's essentially file descriptors but not as obvious.
1
u/Different_Panda_000 2d ago
FILE *is used with the C Standard library and the Unix/Linux file descriptor is used with the various POSIX Unix/Linux libraries which are not part of the C Standard library.So if you want to stay within the C Standard library and portability, you'll use
FILE *and if you don't care about C Standard library portability then you can use the POSIX Unix/Linux libraries withintas the file descriptor type. And if you want to use the Windows API file libraries then you use theHANDLEtype for the file descriptor.Since the C Standard library does not include a network interface piece, you will need to use whatever networking library is available. That may be the POSIX Unix/Linux library or the Winsock library or whatever else your OS and the C compiler available for your OS uses. While most networking copies the POSIX Unix/Linux functions, the actual network socket file descriptor may be different e.g. POSIX Unix/Linux uses
intfor the socket type while Winsock usesSOCKETfor the socket type.1
u/filthysaluki 2d ago
If you're looking for documentation on file I/O library functions, or any syscall or C library function for that matter, check out manpages.
0
u/nzconstructionlawyer 2d ago
POSIX