Readfile's 2nd parameter, Problem.

This is a discussion on Readfile's 2nd parameter, Problem. within the Windows Programming forums, part of the Platform Specific Boards category; Code: int wmain() { HANDLE hd = CreateFile(L"myfiled.txt",GENERIC_ALL,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); CHAR buff[200]; DWORD written; int m=ReadFile(hd,buff,200,&written,NULL); if(!m) { cout<<"Read file error"; return ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    Readfile's 2nd parameter, Problem.

    Code:
    int wmain()
    {
        
    
        HANDLE hd = CreateFile(L"myfiled.txt",GENERIC_ALL,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
        
        CHAR buff[200];
        DWORD written;
    
        
        int m=ReadFile(hd,buff,200,&written,NULL);
        if(!m)
        {
    
            cout<<"Read file error";
            return 0;
        }
        for(int i=0;i<=1998;i++)
        {
            cout<<buff[i];
        }
            
        return 0;
    }
    2nd parameter of Readfile is LPVOID lpBuffer then why are passing char* to it? It is working fine even we have not cast it.

    I think the correct method should be:

    Code:
    int wmain()
    {
        
    
        HANDLE hd = CreateFile(L"myfiled.txt",GENERIC_ALL,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
        
        CHAR buff[200];
        DWORD written;
    
        
        int m=ReadFile(hd,(void*)&buff,200,&written,NULL); // cast to void*
        if(!m)
        {
    
            cout<<"Read file error\n";
            return 0;
        }
        for(int i=0;i<=198;i++)
        {
            cout<<buff[i];  
        }
            
        return 0;
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Any pointer can silently convert to void *
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    Thank You
    Last edited by freiza; 02-27-2012 at 11:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ofstream parameter problem.
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2009, 06:23 AM
  2. Replies: 6
    Last Post: 01-08-2008, 09:25 AM
  3. Simple strcmp parameter problem.....
    By INFERNO2K in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 11:34 AM
  4. Command-line parameter problem
    By Shadow in forum C Programming
    Replies: 3
    Last Post: 04-16-2004, 02:09 PM
  5. Parameter problem
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 07-08-2003, 12:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21