Page 1 of 1

Accessing files as an array in C++?

Posted: Wed Apr 30, 2003 6:22 am
by Perica
..

Re:Accessing files as an array in C++?

Posted: Wed Apr 30, 2003 10:25 am
by Tim
Memory-mapped files.

Under Windows, you do:
CreateFile (open the file)
CreateFileMapping (create a file mapping object on the file)
MapViewOfFile (open a memory window into the file mapping object)
Access the file as an array
UnMapViewOfFile
CloseHandle(file_mapping)
CloseHandle(file)

Re:Accessing files as an array in C++?

Posted: Thu May 01, 2003 3:52 am
by Perica
..

Re:Accessing files as an array in C++?

Posted: Thu May 01, 2003 4:28 pm
by Tim
You have no choice but to use the Win32 API for this, as the C standard library doesn't include memory-mapped files.

However, you can use fopen/fread/fwrite/fclose/fseek to do 'normal' file I/O under standard C. They come from the <stdio.h> file. Alternatively, you can use the C++ library; look up the std::ifstream and std::ofstream classes.

Re:Accessing files as an array in C++?

Posted: Thu May 01, 2003 11:27 pm
by Andrew_Baker
If you're using C++, can't you build an array of file _objects_? I'm not too sure of the implementation, but that's what I would suggest.

or could it be as simple as f_array[x]=fopen("whatever","r")?

Re:Accessing files as an array in C++?

Posted: Fri May 02, 2003 7:47 am
by Perica
..

Re:Accessing files as an array in C++?

Posted: Fri May 02, 2003 11:13 am
by Tim
Sure, but memory-mapped files are far more efficient, especially for larger files.

Why do you need all of the file in memory at once? What's stopping you reading only what's needed?

Re:Accessing files as an array in C++?

Posted: Fri May 02, 2003 10:33 pm
by Perica
..

Re:Accessing files as an array in C++?

Posted: Sat May 03, 2003 3:59 am
by Tim