Thread: need help on OpenFile()

  1. #1
    Unregistered
    Guest

    need help on OpenFile()

    can anyone help give an example of this function for opening a file for reading access?

    I am new to this and want to avoid using fstream.h

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    OpenFile() has been superseded by CreateFile()
    This will return the HANDLE to a file given the path, including the name of the file. It will display a message box if it fails (with the error code).
    Code:
    HANDLE	OpenDocument(HWND hDlg, char *sFilePath)
    {
    	char				sBuffer[STRING];
    	int					iError=INVALID;
    	HANDLE				hFile;
    
    	hFile=CreateFile(sFilePath, GENERIC_WRITE|GENERIC_READ ,0 ,NULL ,OPEN_ALWAYS ,FILE_ATTRIBUTE_NORMAL ,NULL);
    	if((hFile == NULL)||(hFile == INVALID_HANDLE_VALUE))
    	{
    		iError=GetLastError();
    		sprintf(sBuffer,"%s file failed to open with Error #:%d",sFilePath,iError);
    		MessageBox(hDlg,sBuffer,"MyAppError",MB_ICONERROR|MB_OK);
    		return NULL;
    	}
    	else
    	{
    		return hFile;
    	}
    }
    Look up the flags I have used as I want the file to be created if it does not exist and may want to write to it.
    Use Readfile() and SetFilePointer() to read data from the file.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. openfile error
    By lambs4 in forum Windows Programming
    Replies: 4
    Last Post: 11-06-2002, 09:00 PM
  3. BC++5 can not open OpenFile dialog
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 05-22-2002, 11:27 AM
  4. OpenFile API
    By Steve-O in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2002, 04:12 PM
  5. OpenFile Dialog Box
    By Terrell in forum Windows Programming
    Replies: 5
    Last Post: 09-30-2001, 01:52 PM