I want to read from a file by using its file descriptor. I can't use its name because of assignment rules.
I obtain it by calling open
and it works fine. At this moment I know that I have to use the read()
function in order to read from it. My problem is that read()
function requires as an argument the number of bytes to read, and I want to read a whole line from the file each time, so I don't know how many bytes to read.
If i use for example fscanf()
, it works fine with a simple string and I take back the whole line as I want. So my question is:
Is there any function like fscanf()
which can be called with file descriptor and not with a file pointer?
When you say "have to use read()
" I can't tell if that's your understanding of the situation given a file descriptor from open()
or a restriction on some kind of assignment.
If you have a file descriptor but you're more comfortable with fscanf()
and friends, use fdopen()
to get a FILE *
from your fd and proceed to use stdio.
Internally it uses functions like read()
into a buffer and then processes those buffers as you read them with fscanf()
and friends.