Thread: ReadFile() and SetFilePointer Problems?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29

    ReadFile() and SetFilePointer Problems?

    Hii

    In my program I'm reading the clusters in the volume using the appropriate physical offsets to the disk.



    Code:
    for(  i=0; i<  total_number_ of_ clusters;i++)
    {
      
    // convert logical offset to physical offset 
    
     phys_offset= conversion(i);  // this value I get correctly
    
    
     if(!SetFilePointer(drive_handle, 
                                 phys_offset,
                                 NULL,
                                 0))
    {
     printf("\nSetFilePointer Failed: Error Number : %d\n",GetLastError());
     return o;
     }
    
    
     if(!ReadFile(drive_handle,
                        bBuffer1,
                        cluster_size,
                        &dwRetBytes1,
                        0))
    {
    printf("\nUnable to Read the Drive Error: %d\n",GetLastError());   
    return o;
    }
       
    
    } // next cluster

    The above code not giving no error messages, but how do we know the program reading correctly?
    It returns the correct no of bytes read after one read operation .
    When I wrote theses data into destination , nothing wrote.

    What may be the problem??
    Where is the mistake in the code ?


    Any idea?
    -----------
    Thanking you
    Krish

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well then if the above code is working properly, why not post the part where you actually attempted to write it somewhere?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    According to MSDN docs, SetFilePointer doesn't return a boolean (and in fact, returns 0xFFFFFFFF on error). Obviously, then, the function is failing in your code...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    I mean no compilation and link errors.. the code is doesn't give correct result I want.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    Mr...

    You are correct....I corrected my code as below

    Code:
      DWORD dwPtr1=SetFilePointer(drive_handle,phys_offset,NULL,0);
           if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
    	   {
    	   printf("\nSetFilePointer Failed to write,Err No:%d\n",GetLastError());
    	   goto  label12;
    	   }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> You are correct....I corrected my code as below

    So...everything is working properly then??

    >> SetFilePointer(drive_handle,phys_offset,NULL,0)

    The last parameter should be FILE_BEGIN, FILE_CURRENT, or FILE_END. Using 0 is not portable,and obscures the meaning.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    but I can't set the file pointer....to write the read data

    And my physical offsets are correct ,I verified them....


    the physical offset is 58070509056


    While using set pointer () the error occurs.



    ERROR_NEGATIVE_SEEK 131 0x83

    An attempt was made to move the file pointer before the beginning of the file.


    What may be the problem???

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    hiii

    Code:
    
        #define FILE_BEGIN           0
       #define FILE_CURRENT       1
       #define FILE_END                2
    
    
    // the above are defined in <winbase.h>
    

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> the physical offset is 58070509056

    A DWORD can only hold a value up to 4294967295. You'll probably need to use that 3rd parameter to SetFilePointer then, don't you think?

    >> // the above are defined in <winbase.h>

    There is nothing stopping the API from changing these values in future versions, so you should always stick with the pre-defined macros, for portability.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    If the function succeeds and lpDistanceToMoveHigh is NULL, the return value is the low-order DWORD of the new file pointer.

    DWORD WINAPI SetFilePointer(
    __in HANDLE hFile,
    __in LONG lDistanceToMove, //58070509056
    __inout_opt PLONG lpDistanceToMoveHigh, //NULL
    __in DWORD dwMoveMethod //FILE_BEGIN
    );


    The how it becomes an error code?

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You need to read the reponses to your questions more carefully. I've already pointed this out to you, and so I am just repeating myself here:

    >> __in LONG lDistanceToMove, //58070509056

    OK, once again, you *cannot* fit that number into a 32 bit variable.

    >> The how it becomes an error code?

    It becomes an error code by being 0xffffffff.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    I cleared my code as follows


    Code:
    DWORD dwPtr1=SetFilePointer(hDevice1,
                                                     li_offset2.LowPart,
                                                    &li_offset2.HighPart,
                                                    FILE_BEGIN);
    
    if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
    {
    printf("\n\nSetFilePointer Failed to write,Err No:%d\n",GetLastError());
    return 0;
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    There's something you're all missing.
    ReadFile() and SetFilePointer Problems? - C++

    The problem is the OP.
    They're just scattering fragments of the same question all over the place, but when they provide "new information", they neglect to tell everyone else about it as well.

    So pretty much everyone spends (read: wastes) their time wandering around in the dark.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An fgets() implementation using ReadFile()
    By bodom in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2004, 11:27 PM
  2. Using API function ReadFile()
    By Echidna in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2001, 10:33 PM