简体   繁体   English

这会复制我的二进制文件中的所有数据吗?

[英]Is this going to copy all the data in my binary files?

I have a huge array of string stored as binary file.我有一个巨大的字符串数组存储为二进制文件。

std::string strArr[1000];
std::ifstream file("strFile.bin",std::ios:: binary);
file.read((char*)&strArr,sizeof(strArr));
std::cout<<strArr[500];

Will that code copy all the data from the binary file?该代码会复制二进制文件中的所有数据吗? If so, how can I make it not copy so if I output a string it will just get it directly from file.如果是这样,我怎样才能让它不复制,所以如果我 output 一个字符串,它将直接从文件中获取它。 Kinda pointer thing.有点指针的东西。 I really appreciate someone will help, thanks!我真的很感谢有人会提供帮助,谢谢!

Your code is incorrect.您的代码不正确。 It is creating a 1000 strings.它正在创建 1000 个字符串。 This is the correct form:这是正确的形式:

    std::string strArr(1000, 0);
    std::ifstream file("strFile.bin", std::ios::binary);
    file.read(strArr.data(), strArr.size());

It will read 1000 bytes from your file (not all the data).它将从您的文件中读取 1000 个字节(不是所有数据)。 Yes it will copy.是的,它会复制。 If you don't want to copy, you can use file memory mapping on linux (see this ).如果您不想复制,可以使用 linux 上的文件 memory 映射(请参阅)。

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

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