Thread: File Acess under Windows (Using MSVC++ 6)

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    File Acess under Windows (Using MSVC++ 6)

    When Opening a File (using stream i/o)

    How could i acess a file as an array, for example:
    Code:
    var = file[0];
    -- How would i do something like that?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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;
    }

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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
    He asked a question how to address a file as memory, and I gave an example....what's the problem?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Settle down Fordy....just keying off of
    (using stream i/o)
    Perica, 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.

    gg

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Codeplug
    Settle down Fordy....just keying off of
    Sorry.....I was a bit snappy.....more coffee needed

  7. #7
    |<o>| <--cyclopse LouDu's Avatar
    Join Date
    Mar 2003
    Posts
    137
    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. */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM