简体   繁体   中英

how to read last n bytes from a proc file in C

I want to read last n bytes of proc file /proc//status.

On regular files, I can fseek from the end of the file like:

fseek(proc_file, -BUF_SIZE, SEEK_END);

but since the proc file has zero size, this doesn't work.

Any suggestions on how to read from the end ?

I would definite want to avoid looping till the end.

The status file is small. Just fread the first 10000 bytes:

int fileLen = fread( statusFile, buffer, 10000 );

fread will return the number of bytes in the file along with the contents of the file. You can then check the end of the file.

There is no advantage to reading just the last few bytes. File i/o systems are buffered, such that asking even for a few bytes will cause the lower layers to read a couple of k anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM