Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SetFilePointer() + WriteFile() Problems?

SetFilePointer() + WriteFile() Problems?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krish_kumar
    wrote on last edited by
    #1

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

    drive_handle=Createfile(L"\\\\.\\PhysicalDrive0",......);

    for( i=0;total_noof_clusters;i++)//assign 0 as logical startling offset.
    {
    // convert cluster logical offset to physical offset
    // (source& dest same size,same file system)

    phys_offset_source= conversion(i); // this value I get correctly
    phys_offset_dest= conversion(i); // this value I get correctly

    DWORD dwPtr1=SetFilePointer(drive_handle,phys_offset_source,NULL,0);
    if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
    {
    printf("\nSetFilePointer Failed to read from source,:%d\n",GetLastError());
    return 0;
    }

    if(!ReadFile(drive_handle,
    bBuffer1,
    cluster_size,
    &dwRetBytes1,
    0))
    {
    printf("\nUnable to Read the Disk Error: %d\n",GetLastError());
    return o;
    }

    DWORD dwPtr2=SetFilePointer(drive_handle,phys_offset_dest,NULL,0);

    if (dwPtr2 == INVALID_SET_FILE_POINTER) // Test for failure
    {
    printf("\nSetFilePointer Failed to write position%d\n",GetLastError());
    return 0;
    }

    if(!WriteFile(drive_handle,
    bBuffer1,
    cluster_size,
    &dwRetBytes1,
    0))
    {
    printf("\nUnable to write he Drive Error: %d\n",GetLastError());
    return o;
    }

    } // next cluster

    The above code not giving no (compilation and link)error messages, But does not give the result. 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 krishnampkkm is online now Report Post Edit/Delete Message

    C S 2 Replies Last reply
    0
    • K krish_kumar

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

      drive_handle=Createfile(L"\\\\.\\PhysicalDrive0",......);

      for( i=0;total_noof_clusters;i++)//assign 0 as logical startling offset.
      {
      // convert cluster logical offset to physical offset
      // (source& dest same size,same file system)

      phys_offset_source= conversion(i); // this value I get correctly
      phys_offset_dest= conversion(i); // this value I get correctly

      DWORD dwPtr1=SetFilePointer(drive_handle,phys_offset_source,NULL,0);
      if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
      {
      printf("\nSetFilePointer Failed to read from source,:%d\n",GetLastError());
      return 0;
      }

      if(!ReadFile(drive_handle,
      bBuffer1,
      cluster_size,
      &dwRetBytes1,
      0))
      {
      printf("\nUnable to Read the Disk Error: %d\n",GetLastError());
      return o;
      }

      DWORD dwPtr2=SetFilePointer(drive_handle,phys_offset_dest,NULL,0);

      if (dwPtr2 == INVALID_SET_FILE_POINTER) // Test for failure
      {
      printf("\nSetFilePointer Failed to write position%d\n",GetLastError());
      return 0;
      }

      if(!WriteFile(drive_handle,
      bBuffer1,
      cluster_size,
      &dwRetBytes1,
      0))
      {
      printf("\nUnable to write he Drive Error: %d\n",GetLastError());
      return o;
      }

      } // next cluster

      The above code not giving no (compilation and link)error messages, But does not give the result. 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 krishnampkkm is online now Report Post Edit/Delete Message

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      krish_kumar wrote:

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

      I could not understand how could achieve this using SetFilePointer, that uses an offset relative to the start of the file. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      K 1 Reply Last reply
      0
      • K krish_kumar

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

        drive_handle=Createfile(L"\\\\.\\PhysicalDrive0",......);

        for( i=0;total_noof_clusters;i++)//assign 0 as logical startling offset.
        {
        // convert cluster logical offset to physical offset
        // (source& dest same size,same file system)

        phys_offset_source= conversion(i); // this value I get correctly
        phys_offset_dest= conversion(i); // this value I get correctly

        DWORD dwPtr1=SetFilePointer(drive_handle,phys_offset_source,NULL,0);
        if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
        {
        printf("\nSetFilePointer Failed to read from source,:%d\n",GetLastError());
        return 0;
        }

        if(!ReadFile(drive_handle,
        bBuffer1,
        cluster_size,
        &dwRetBytes1,
        0))
        {
        printf("\nUnable to Read the Disk Error: %d\n",GetLastError());
        return o;
        }

        DWORD dwPtr2=SetFilePointer(drive_handle,phys_offset_dest,NULL,0);

        if (dwPtr2 == INVALID_SET_FILE_POINTER) // Test for failure
        {
        printf("\nSetFilePointer Failed to write position%d\n",GetLastError());
        return 0;
        }

        if(!WriteFile(drive_handle,
        bBuffer1,
        cluster_size,
        &dwRetBytes1,
        0))
        {
        printf("\nUnable to write he Drive Error: %d\n",GetLastError());
        return o;
        }

        } // next cluster

        The above code not giving no (compilation and link)error messages, But does not give the result. 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 krishnampkkm is online now Report Post Edit/Delete Message

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        Are you using Vista? If so, are you meeting the conditions specified by this page[^]?

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        K 1 Reply Last reply
        0
        • C CPallini

          krish_kumar wrote:

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

          I could not understand how could achieve this using SetFilePointer, that uses an offset relative to the start of the file. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          K Offline
          K Offline
          krish_kumar
          wrote on last edited by
          #4

          Hiiii As per MSDN the condition is "file handle must be created with the GENERIC_READ or GENERIC_WRITE". Here I opened a Hard disk handle using NTFs in windows xp. ------------ thanking you ------------

          C 1 Reply Last reply
          0
          • S Stuart Dootson

            Are you using Vista? If so, are you meeting the conditions specified by this page[^]?

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            K Offline
            K Offline
            krish_kumar
            wrote on last edited by
            #5

            And it always shows ..... the error Error 131 - Error Code 0x83 Windows Error Message 131: 'ERROR_NEGATIVE_SEEK' An attempt was made to move the file pointer before the beginning of the file. I am changing the setfilepointer() code ...but result is same...same error displaying How to solve this...? ------------ thanking you ------------

            S 1 Reply Last reply
            0
            • K krish_kumar

              Hiiii As per MSDN the condition is "file handle must be created with the GENERIC_READ or GENERIC_WRITE". Here I opened a Hard disk handle using NTFs in windows xp. ------------ thanking you ------------

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Well, such a sentence, without context, does not provide any evidence of. Could you please post the link? :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • K krish_kumar

                And it always shows ..... the error Error 131 - Error Code 0x83 Windows Error Message 131: 'ERROR_NEGATIVE_SEEK' An attempt was made to move the file pointer before the beginning of the file. I am changing the setfilepointer() code ...but result is same...same error displaying How to solve this...? ------------ thanking you ------------

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                You do realise that by only using the low-order DWORD in SetFilePointer, you're limited to the first 2GB of the disk, I presume? What offset are you feeding to SetFilePointer?

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                K 1 Reply Last reply
                0
                • S Stuart Dootson

                  You do realise that by only using the low-order DWORD in SetFilePointer, you're limited to the first 2GB of the disk, I presume? What offset are you feeding to SetFilePointer?

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  K Offline
                  K Offline
                  krish_kumar
                  wrote on last edited by
                  #8

                  I have tried offset as LARGE_INTEGER alsooo.....but same error...

                  S 1 Reply Last reply
                  0
                  • K krish_kumar

                    I have tried offset as LARGE_INTEGER alsooo.....but same error...

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Passing the low and high words separately as specified by the documentation? You can't just pass a LARGE_INTEGER or LARGE_INTEGER pointer.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    K S 2 Replies Last reply
                    0
                    • S Stuart Dootson

                      Passing the low and high words separately as specified by the documentation? You can't just pass a LARGE_INTEGER or LARGE_INTEGER pointer.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      K Offline
                      K Offline
                      krish_kumar
                      wrote on last edited by
                      #10

                      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;
                      }

                      1 Reply Last reply
                      0
                      • S Stuart Dootson

                        Passing the low and high words separately as specified by the documentation? You can't just pass a LARGE_INTEGER or LARGE_INTEGER pointer.

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #11

                        Have you actually checked what the value is that you're passing to it? It's entirely possible that SetFilePointer isn't lying and that you are passing a negative number to it?

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        K 1 Reply Last reply
                        0
                        • S Stuart Dootson

                          Have you actually checked what the value is that you're passing to it? It's entirely possible that SetFilePointer isn't lying and that you are passing a negative number to it?

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                          K Offline
                          K Offline
                          krish_kumar
                          wrote on last edited by
                          #12

                          Thank uu.. thank uu thank uu I was passing -ve value......... Mr.Stuart Dootson U are really great...... :laugh: :laugh: :laugh: -------------- Thanking you --------------

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups