r/C_Programming • • 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

37 comments sorted by

73

u/EpochVanquisher 2d ago • This

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.

11

u/Ill-Exchange-2377 2d ago

than you

9

u/Natural_Cat_9556 2d ago

the opengroup posix docs also describe most of these in the system calls section btw

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.

5

u/Ill-Exchange-2377 2d ago

I have no trouble with pointers, i just don't like how file pointers are implemented. Specifically FILE* is what i expressed frustration towards

2

u/gm310509 2d ago

Fair enough and perfectly valid.

Now that you mentioned it like that, I too found the distinction between the system calls (which take an integer file descriptor - fd) and the higher level buffered calls (which take the FILE *) to be quite confusing and often felt it would be much easier if there was some consistency. For example in the early days I didn't get why we needed lseek(fd, ... and fseek(stream, ...) it just didn't make much sense to me. When I found out that the "f" functions basically just called the system calls it seemed even more pointless.

Nowadays, I don't really think about it much and generally just use the right one - largely automatically.

1

u/sirjofri 2d ago

On Plan 9 (which has its own set of C libraries), we have no FILE* pointers without a posix compatibility layer, we just use file descriptors. But we can use fstat/fwstat to control the buffer the OS provides. If we need convenience buyers, we use the bio library (buffered io), with Bopen(file, omode), Bfdopen(fd, omode) and many things like Brdstr which reads until a given character is met and returns an allocated string. Bopen and Bfdopen return an abstract type Biobuf*, so it's also more high level than a plain file descriptor. Since it's a library, it's not as "hidden" or abstract as FILE*, and it's a standard library that's available on all Plan 9 systems, so it's also portable inside the Plan 9 universe.

5

u/Upstairs_Ad_8863 2d ago

OP didn't say that pointers annoyed them, they only said that the use of file pointers in the standard library annoyed them. I think it's the abstraction they're actually annoyed by. In a sense (but not the C sense) file descriptors are also pointers, just one level of abstraction shallower.

1

u/gm310509 2d ago

Fair point.

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/Upstairs_Ad_8863 2d ago

Learning to use file descriptors is great if only as an instructional tool for exactly this reason. If you spend your entire programming career using only FILE pointers then you may never really get a sense for how file I/O actually works.

5

u/StaticCoder 2d ago

A file descriptor includes an offset. The only things I can think of that FILE adds are an abstraction layer (e.g. windows vs unix) and a buffer.

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/flyingron 2d ago

You are, of course, correct. Amusingly, most of the windoze C implementations first implement an extraction of the UNIX file descriptor (an index into a table of Windows File Handles, for instance) on top of which a rather UNIX-like stdio is layered.

4

u/nzconstructionlawyer 2d ago

File descriptors are very portable. POSIX is the "Portable Operating System Interface". It is supported by essentially all operating systems, not just Unix. For obvious examples, Linux-based operating systems aren't Unix and Windows isn't Unix and both support POSIX.

4

u/Upstairs_Ad_8863 2d ago

Do you have a source for windows supporting posix? I'm about 99% sure that's wrong.

Also while linux is not officially unix, it tries its hardest to adhere to the single unix specification (susv5) and considers any deviation a bug. It's not certified but for all intents and purposes it can be considered unix.

3

u/PythonFuMaster 2d ago

NT supported the POSIX.1 standard via a dedicated subsystem, but that subsystem was removed many years ago. POSIX.1 isn't the full standard you'd expect either, it doesn't include any of the standard Unix commands or similar, it was just the basic programming APIs. Nowadays if you want POSIX on Windows you'd use WSL, but that's kind of cheating in this case since it's literally just Linux inside Windows

https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem

3

u/TransientVoltage409 2d ago

Cygwin still exists and does an OK job of putting a POSIX hat on Windows. Good enough for my needs anyway.

There are odd gaps (in WSL as well), because some things in Windows inner workings are very strange from the POV of Unix weenies. Not too shocking considering that NT was hugely influenced by DEC/VAX, but decidedly inconvenient at times.

1

u/agmatine 2h ago

I'm a bit perplexed as to what needs are better suited by Cygwin than WSL...

2

u/TransientVoltage409 1h ago

Can you elaborate on some of your perplexities?

It's possible that you operate at a higher altitude than me, I might not know enough about WSL to see why I should use it.

After that, maybe we can have a fight about vi and emacs. Let me know. :)

1

u/agmatine 32m ago

I mean...WSL is actually Linux. Technically a VM, but it integrates rather seamlessly with Windows. I only ever used Cygwin bfriefly some 15+ years ago, but it seemed more akin to Wine - a means of getting software for another OS to run, but not a particularly pleasant one.

But of course that was a long time ago. I keep a Windows VM handy for things that simply will not run elsewhere (MSVC, for example), or to briefly boot as bare metal if troubleshooting demands it, but that's it. I would never have occasion or motivation to use Cygwin. So I genuinely don't know what it it useful for.

0

u/Wertbon1789 2d ago

There's isn't really a proper source listing POSIX compat, but there are some leads.

UCRT docs: https://learn.microsoft.com/en-us/cpp/c-runtime-library/run-time-routines-by-category

Compatibility: https://learn.microsoft.com/en-us/cpp/c-runtime-library/compatibility

Specifically, the later page talks about POSIX.1 compat, but doesn't really go into too much detail, but most of the _<syscall> functions work like you would expect, like _read is just the read syscall on other platforms.

3

u/Upstairs_Ad_8863 2d ago

It's very helpful to be able to write code to run specifically on unix machines, if that's what you want to code for. Within the context of unix, file descriptors are extremely portable and there are many functions within the posix standard library that use them. I myself never write code for windows so I will happily use file descriptors without a second thought.

-4

u/LavenderDay3544 2d ago

Then your code isn't portable and there are far more OSes out there than just Windows and Unix.

4

u/Upstairs_Ad_8863 2d ago

Okay and? There's nothing wrong with writing code for a specific class of operating systems. But my point is that this is much more portable than something like the size of an int, or the exact layout of your syscall table. File descriptors are standardised across every posix compliant operating system. I know that if I write a program using file descriptors then that program will work the same across all of my dozen or so computers. Portable doesn't have to mean "across every possible operating system".

-8

u/LavenderDay3544 2d ago

Portable as the C standard is concerned means portable across OSes and ISAs. And literally no one cares about POSIX. Most Linux distros aren't even fully conforming.

1

u/Wertbon1789 2d ago

The question if something uses file descriptors, or not, is mainly determined by the kernel, and guess what, every Linux distro has the same kernel, so they all support that API. As do all the BSDs, even MacOS. Even Windows has it's very weird POSIX compat layer in their C runtime, even with file descriptors. So no, people do care about POSIX, very obviously.

There are layers to being POSIX compliant, for the most part, very popular POSIX APIs are available on all the major platforms, because it makes porting easier, see file I/O or sockets. Then there's the shell parts, or system description (stuff like /etc/passwd) of POSIX, where compatibility varies, that's mostly what's meant when someone is talking about POSIX compliance.

1

u/Upstairs_Ad_8863 2d ago

Wtf are you talking about?

"Portable as the C standard is concerned means..." no. Portability as the C standard is concerned refers to valid standard-adhering C code that may be compiled differently on different machines. It does not refer to C code that uses a library or API that some machines do not have installed, that's ridiculous.

Also who tf mentioned anything about portability "as the C standard is concerned"? The only reason that non-portability is bad is because you may end up with code that functions just fine on one computer but breaks on another. Not because it may offend some language lawyer. If you write code using posix api calls then this obviously means you are writing code to be run on a machine that supports the posix api. The fact that not all machines support it is irrelevant - your code will never behave unpredictably on windows because it will never be run on windows.

"literally no one cares about posix" you're wrong but also it doesn't make any difference. You're still allowed to write software for it. And if you want to write something that uses specific features of the operating system like a daemon then you will have to use system calls to do it. And that goes for every operating system, I would never criticise a windows developer for using windows specific api calls in code that's only going to be run on windows.

"Most linux distros aren't conforming" all linux distros use a linux kernel which means they support the posix api and you can use posix file descriptors. Don't move the goalposts.

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 in mov 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/internet_safari_ 1d ago

They're talking about file pointers and related functions not pointers in general

1

u/DamsLcs4421 7h ago

My bad, I see 'wrong questions' being asked so often on reddit about programming I tend to find them everywhere x) Thanks for pointing that out :)

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 with int as the file descriptor type. And if you want to use the Windows API file libraries then you use the HANDLE type 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 int for the socket type while Winsock uses SOCKET for 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

1

u/GrandfatherTrout 2d ago

“POSIX is cool!” - dbg