When Opening a File (using stream i/o)
How could i acess a file as an array, for example:-- How would i do something like that?Code:var = file[0];
When Opening a File (using stream i/o)
How could i acess a file as an array, for example:-- How would i do something like that?Code:var = file[0];
If you are using VC++, you can use the file mapping apis......they map a filoe into a contiguous area of memory (just like when loading an exe or dll)
Create a file called MyFile.txt, Write "Hello World" into it and save it....the run this prog in the same directory...it will map the file.....copy its contents from 1 memory loaction to another, and then change the mapped memory - this change will be reflected in the file when you quit the program
Code:#include <windows.h> int main() { HANDLE hFile = CreateFile("MyFile.txt", GENERIC_READ | GENERIC_WRITE,0,0, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); if(hFile != INVALID_HANDLE_VALUE) { HANDLE hMap = CreateFileMapping(hFile,0,PAGE_READWRITE,0,0,0); if(hMap != 0) { char* Buff = static_cast<char*>(MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS,0,0,0)); if(Buff != 0) { DWORD Size = GetFileSize(hFile,0); if(Size != 0xFFFFFFFF) { char* String = new char[Size+1]; if(String != 0) { CopyMemory(String,Buff,Size); String[Size] = '\0'; MessageBox(0,String,"Result",MB_OK); Buff[0] = 'M'; CopyMemory(String,Buff,Size); MessageBox(0,String,"On Change",MB_OK); delete [] String; } else { MessageBox(0,"Unable to alloc space",0,MB_OK); } } else { MessageBox(0,"Unable to size file",0,MB_OK); } } else { MessageBox(0,"Unable to map file",0,MB_OK); } CloseHandle(hMap); } else { MessageBox(0,"Unable to open map",0,MB_OK); } CloseHandle(hFile); } else { MessageBox(0,"Unable to open file",0,MB_OK); } return 0; }
Once you've impressed all your friends with knowledge of memory mapped files under Win32 - you can just read the file into memory using operators/methods like >>, get(), getline(), and read().
Write some code and come back and ask for help if you are still stuck.
gg
He asked a question how to address a file as memory, and I gave an example....what's the problem?Originally posted by Codeplug
Once you've impressed all your friends with knowledge of memory mapped files under Win32 - you can just read the file into memory using operators/methods like >>, get(), getline(), and read().
Write some code and come back and ask for help if you are still stuck.
gg
Settle down Fordy....just keying off ofPerica, there is no standards (using stream i/o) way of mapping a file into memory - the OS will have to help you there. If you were looking for just such a solution, Fordy has given you a BIG head start.(using stream i/o)
gg
Sorry.....I was a bit snappy.....more coffee neededOriginally posted by Codeplug
Settle down Fordy....just keying off of
fordy is alwsys snappy
Languages <> C++, HTML
/*The Ledgend of Ludlow Coming to a PC Near
You intro Cinenmatic Needed email me of
Reply to one of my threads if you can make
one, thats decent. */