getline() with a file descriptor instead of a file pointer
Asked Answered
S

1

11

To my knowledge, there is no libc equivalent to getline() that works with a file descriptor instead of working with a FILE *.

Is there a (technical) reason for that?

Spohr answered 3/12, 2015 at 10:52 Comment(2)
getline is not from the C standard, I suppose you mean the POSIX feature?Chervil
Term file descriptor is so subjective that it can be any thing from an IOFile to a socket. so read line may not be a correct method to implement unless its very much related to FILEsWaterbuck
O
15

You can create a FILE stream out of a filedescriptor with fdopen.

To generically get a line out of a file descriptor, you'd need to ask the OS for one character at a time and that is very inefficient. (The read builtin in POSIX shells works like this—it reads lines very inefficiently by retrieving a byte at a time.)

FILE streams ask for data from the OS in batches, which improves efficiency, however the file descriptor might not be a rewindable file—it might be a socket or a pipe and if you ask for 100 characters and the third character of that 100 batch is the newline character, then there's no way to generically undo the read of the 97 characters after it.

Okra answered 3/12, 2015 at 11:14 Comment(5)
You would not need to ask the OS one char at a time, a simple implementation is possible using a static buffer to fetch as many bytes at a time as you want from the OS. This was actually a coding exercise in the school I did my studies in, Epitech, called get_next_line. I'm sure you can find plenty of student projects by that name on GitHub. Note that, if you want to use one of them, you should get one that keeps the file descriptors in an array to be able to handle several files at a time.Kuhl
@Kuhl Sure. But then you'll have read more than just the one line, which means your subsequent accessses to the file will be off, unless you a) keep using your buffering layer b) rewind. And b) is not always possible, because not all files are rewindable.Okra
Yes, that's why I was talking about a static buffer, to reuse it in subsequent calls and consume the lines already present in the buffer before fetching new data. Maybe some work would be needed to make it reliable between close() and open() giving the same fd for a different file, but my point is just that it is technically possible.Kuhl
Was there a particular issue that you were thinking of with using a static buffer to retain the data ?Kuhl
@Kuhl You solution is conceptually equivalent to using stdio. It also binds you to a particular family of function that access the fd through the buffer, or otherwise your fd accesses will go out of sync with the buffer.Okra

© 2022 - 2024 — McMap. All rights reserved.