简体   繁体   中英

File Mapping,How to open a file(txt) from a specific location

I have a big .txt file (over 1gb). While searching a way to open it fast I found mapping.

I managed to use CreateFile() , then I made a char buffer[] and finally put the file contents in the buffer with ReadFile() . The problem is that the file is too big, so I can't load it all at once into the buffer, because I can't make an array that big.

I think the solution would be to open and close the file at specified locations in the .txt file and get a few of the file contents each time. The only source I found explaining mapping was on MSDN but I can't find out how to do it.

So in the end, how do I read a big file with a mapping?

HANDLE my_File = CreateFileA("words.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
if (my_File == INVALID_HANDLE_VALUE)
{
    cout << "Failed to open file" << endl;
    return 0;
}
    
constexpr size_t BUFFSIZE = 1000000;
    
char buffer[BUFFSIZE];
DWORD dwBytesToRead = BUFFSIZE - 1;
DWORD dwBytesRead = 0;
    
BOOL my_Bool = ReadFile(my_File,(void*)buffer, dwBytesToRead, &dwBytesRead, NULL);
    
if (dwBytesRead > 0)
{
    buffer[dwBytesRead] = '\0';
    cout << "FILE IS: " << buffer << endl;
}
    
CloseHandle(my_File);

I think you are confused. The whole purpose of mapping part or all of a file into memory is to avoid the need to buffer the data yourself. Instead, the OS takes care of that for you, allowing you to access the contents of the file via a pointer, just like you would any other in-memory data structure.

Only you can decide if that's the best solution for you. In a 32 bit app, 1GB is a lot of addressing space to find. In a 64 bit app there is no such problem. As mentioned in the comments, reading the file in chunks into a smaller buffer can be a better bet, especially if you want to process it sequentially.


For some example code on how to memory map a file, see:

How to CreateFileMapping in C++?

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