简体   繁体   English

构造iostream(c ++)是否将数据从硬盘驱动器读入内存?

[英]Does constructing an iostream (c++) read data from the hard drive into memory?

When I construct an iostream when say opening a file will this always read the entire file from the hard disk and then put it into memory, or is it streamed in and buffered by the OS on demand? 当我说要打开文件时构造iostream时,这将始终从硬盘读取整个文件,然后将其放入内存,还是按需将其流化并由OS缓冲?

I ask because one way to check if a file exists is to see if opening it fails, but I fear if the files I am opening are very large then this take a long time if iostream must read the entire file in on open. 我问,因为一种检查文件是否存在的方法是查看打开文件是否失败,但是我担心如果我打开的文件很大,那么如果iostream必须在打开时读取整个文件,这将花费很长时间。

To check whether a file exists can be done like this if you want to use boost . 如果要使用boost,可以这样检查文件是否存在。

#include <boost/filesystem.hpp>

bool fileExists = boost::filesystem::exists("foo.txt");

No, it will not read the entire file into memory when you open it. 不,打开文件时不会将整个文件读入内存。 It will read your file in chunks though, but I believe this process will not start until you read the first byte. 虽然它将分块读取您的文件,但我相信直到您读取第一个字节后,该过程才会开始。 Also these chunks are relatively small (on the order of 4-128 kibibytes in size), and the fact it does this will speed things up greatly if you are reading the file sequentially. 而且这些块相对较小(大小为4-128 kb),并且如果您按顺序读取文件,这样做的确会大大加快处理速度。

In a test on my Linux box (well, Linux VM) simply opening the file only results in the OS open system call, but no read system call. 在我的Linux机器(当然是Linux VM)上进行的测试中,仅打开文件只会导致OS open系统调用,而不会导致read系统调用。 It doesn't start reading anything from the file until the first attempt to read from the stream. 在第一次尝试从流中读取之前,它不会开始从文件中读取任何内容。 And then it reads 8191 (why 8191? that seems a very strange number) byte chunks as I read the file in. 然后,当我读入文件时,它读取8191(为什么8191?这是一个非常奇怪的数字)字节块。

Opening a file is a bad way of testing if the file exists - all it does is tell you if you can open it. 打开文件是测试文件是否存在的不好方法-它所做的只是告诉您是否可以打开它。 Opening might fail for a number of reasons, typically because you don't have read permission, but the file will still exist. 可能由于多种原因而导致打开失败,通常是由于您没有读取权限,但文件仍然存在。 It is usually better to use an operating system specific function to test for existence. 通常最好使用操作系统特定的功能来测试其存在性。 And no, opening an fstream will not cause the contents to be read. 不,打开fstream不会导致内容被读取。

What I think is, when you open a file, the corresponding data structures for the process opening the file are populated which include file pointer, file descriptor, v node etc. 我认为是,当您打开文件时,将为打开文件的过程填充相应的数据结构,其中包括文件指针,文件描述符,v节点等。

Now one can read and write to a file using buffered streams (fwrite , fread) or using system calls (read and write). 现在,人们可以使用缓冲流(fwrite,fread)或使用系统调用(读取和写入)来读取和写入文件。

When we use buffered streams, we buffer the data and then write or read it[This is done for efficiency puposes]. 当我们使用缓冲流时,我们缓冲数据,然后写入或读取数据(这样做是出于提高效率的目的)。 This statement itself means that the whole file is not read into memory but certain bytes are read into buffer and then made available. 该语句本身意味着不会将整个文件读入内存,而是将某些字节读入缓冲区然后可用。

In case of sys calls such as read and write , kernel level buffering is done (using fsync one can flush out kernel buffer too), but data is actually read and written to the device .file 如果发生诸如读写之类的sys调用,则完成内核级缓冲(使用fsync也可以清除内核缓冲区),但实际上是将数据读写到设备.file中。

checking existance of file

#include < sys/stat.h >

int main(){
struct stat file_i;
std::string f("myfile.txt");
if (stat(f.c_str(),&file_i) != 0){
   cout << "File not found" << endl;
}
return 0;
}

Hope this clarifies a bit. 希望这可以澄清一点。

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

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