简体   繁体   English

在C中使用fread读取文件

[英]Reading file using fread in C

I lack formal knowledge in Operating systems and C. My questions are as follows. 我缺乏有关操作系统和C的正式知识。我的问题如下。

  1. When I try to read first single byte of a file using fread in C, does the entire disk block containing that byte is brought into memory or just the byte? 当我尝试使用C中的fread读取文件的第一个单字节时,包含该字节的整个磁盘块是否都带入内存或仅带入字节?
  2. If entire block is brought into memory, what happens on reading second byte since the block containing that byte is already in memory?. 如果将整个块都带入内存,那么读取第二个字节会发生什么,因为包含该字节的块已经在内存中了?
  3. Is there significance in reading the file in size of disk blocks? 以磁盘块大小读取文件是否有意义?
  4. Where is the read file block kept in memory? 读取文件块保存在内存中的什么位置?

Here's my answers 这是我的答案

  1. More than 1 block, default caching is 64k. 超过1个块,默认缓存为64k。 setvbuffer can change that. setvbuffer可以改变这一点。
  2. On the second read, there's no I/O. 在二读时,没有I / O。 The data is read from the disk cache. 从磁盘缓存中读取数据。
  3. No, a file is ussuly smaller than it's disk space. 不,文件通常小于磁盘空间。 You'll get an error reading past the file size even if you're within the actual disk space size. 即使超出实际磁盘空间大小,读取文件大小也会出错。
  4. It's part of the FILE structure. 它是FILE结构的一部分。 This is implementation (compiler) specific so don't touch it. 这是特定于实现(编译器)的,因此请不要触摸它。

The above caching is used by the C runtime library not the OS. 上面的缓存由C运行时库而不是OS使用。 The OS may or may not have disk caching and is a separate mechanism. 操作系统可能具有磁盘缓存,也可能没有磁盘缓存,并且是单独的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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