Thread: Readfile's 2nd parameter, Problem.

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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-28-2012 at 12:45 AM.

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, 07:23 AM
  2. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  3. Simple strcmp parameter problem.....
    By INFERNO2K in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 12:34 PM
  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