Lambda Banquo
Lambda Banquo

Reputation: 91

Is there a version of fgets that does not terminate at a newline but instead reads a file for a specified number of characters?

I am using the Linux stat function to find the length of a file in bytes and then I am trying the read the entire file (this is so I don't have to check for a null terminator and can create a perfect sized buffer to store the data) (I am not concerned about the file changing during the read). I tried using fgets but quickly remembered that it terminates at a new line and so this will not work. Is there a version of fgets that does not do this?

Upvotes: 0

Views: 53

Answers (2)

user3629249
user3629249

Reputation: 16540

suggest:

use mmap() to make the contents available via a simple memory reference.

Upvotes: 0

Eric Postpischil
Eric Postpischil

Reputation: 224102

The standard C library routine fread will attempt to read a requested number of bytes (expressed as a number of objects of a given size) from a stream, and the Linux/Unix/POSIX routine read will attempt to read a requested number of bytes (expressed as a number of bytes) from a file descriptor.

Upvotes: 4

Related Questions