简体   繁体   中英

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. Kinda pointer thing. I really appreciate someone will help, thanks!

Your code is incorrect. It is creating a 1000 strings. 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). Yes it will copy. If you don't want to copy, you can use file memory mapping on linux (see this ).

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