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. CreateFile for reading/writing file

CreateFile for reading/writing file

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestion
7 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.
  • P Offline
    P Offline
    Paolo Ponzano
    wrote on last edited by
    #1

    hello, I've the following problem: 1) two files one with data and another one with header about data's file 2) I need to insert the header into the data file at the beginning of file 3) I tried using Createfile in this form tempfd=CreateFile(filename_save, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL); but when I use the ReadFile, it returns error 183, that's defined in msdn ad 183 Cannot create a file when that file already exists. My idea was to copy the content of two files in memory, than paste the header into data file, than append the data file, but doesn't seems to work, any suggestion?? Thanks in advance Paolo

    4 1 Reply Last reply
    0
    • P Paolo Ponzano

      hello, I've the following problem: 1) two files one with data and another one with header about data's file 2) I need to insert the header into the data file at the beginning of file 3) I tried using Createfile in this form tempfd=CreateFile(filename_save, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL); but when I use the ReadFile, it returns error 183, that's defined in msdn ad 183 Cannot create a file when that file already exists. My idea was to copy the content of two files in memory, than paste the header into data file, than append the data file, but doesn't seems to work, any suggestion?? Thanks in advance Paolo

      4 Offline
      4 Offline
      4apai
      wrote on last edited by
      #2

      as i understand the code (pseudo) is following: HANDLE hFile_header = CreateFile(filename_header, ...); HANDLE hFile_save = CreateFile(filename_save, ...); // and we want to append them?! BOOL bRet = ReadFile(hFile_header); // bRet = false? DWORD dwError = GetLastError(); // and dwError = 183? am i right? post code to definite error faster... 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

      P 1 Reply Last reply
      0
      • 4 4apai

        as i understand the code (pseudo) is following: HANDLE hFile_header = CreateFile(filename_header, ...); HANDLE hFile_save = CreateFile(filename_save, ...); // and we want to append them?! BOOL bRet = ReadFile(hFile_header); // bRet = false? DWORD dwError = GetLastError(); // and dwError = 183? am i right? post code to definite error faster... 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

        P Offline
        P Offline
        Paolo Ponzano
        wrote on last edited by
        #3

        yes, you're right, here's my code //those 2 handle are obtained into main program HANDLE source=CreateFile("header", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL); HANDLE destination=CreateFile(filename_save, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL); bool AppendToBeginOfFile(HANDLE source, HANDLE destination) { DWORD source_size=0, destination_size=0, BRs=0, BRd=0, BW=0; char *tmp1=NULL, *tmp2=NULL; if((source == NULL) && (destination == NULL)) return false; source_size=GetFileSize(source, NULL); destination_size=GetFileSize(destination, NULL); tmp1=(char*)malloc(source_size*sizeof(char)); tmp2=(char*)malloc(destination_size*sizeof(char)); bool k=ReadFile(destination, tmp2, destination_size, &BRd, NULL); DWORD l=GetLastError(); k=ReadFile(source, tmp1, source_size, &BRs, NULL); l=GetLastError(); if((BRs < source_size) || (BRd < destination_size)) return false; SetFilePointer(destination, 0 , 0 ,FILE_BEGIN); WriteFile(destination, source, source_size, &BW, NULL); }

        4 B 2 Replies Last reply
        0
        • P Paolo Ponzano

          yes, you're right, here's my code //those 2 handle are obtained into main program HANDLE source=CreateFile("header", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL); HANDLE destination=CreateFile(filename_save, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL); bool AppendToBeginOfFile(HANDLE source, HANDLE destination) { DWORD source_size=0, destination_size=0, BRs=0, BRd=0, BW=0; char *tmp1=NULL, *tmp2=NULL; if((source == NULL) && (destination == NULL)) return false; source_size=GetFileSize(source, NULL); destination_size=GetFileSize(destination, NULL); tmp1=(char*)malloc(source_size*sizeof(char)); tmp2=(char*)malloc(destination_size*sizeof(char)); bool k=ReadFile(destination, tmp2, destination_size, &BRd, NULL); DWORD l=GetLastError(); k=ReadFile(source, tmp1, source_size, &BRs, NULL); l=GetLastError(); if((BRs < source_size) || (BRd < destination_size)) return false; SetFilePointer(destination, 0 , 0 ,FILE_BEGIN); WriteFile(destination, source, source_size, &BW, NULL); }

          4 Offline
          4 Offline
          4apai
          wrote on last edited by
          #4

          man. you open file for writing and try to read it?! investigate createfile function. 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

          P 1 Reply Last reply
          0
          • 4 4apai

            man. you open file for writing and try to read it?! investigate createfile function. 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

            P Offline
            P Offline
            Paolo Ponzano
            wrote on last edited by
            #5

            I know, it's not possible to obtain an handle for both ? ReadFile and WriteFile?? I'll do a CloseHandle and reopen with read permission, seems the only way to me, thanks

            4 1 Reply Last reply
            0
            • P Paolo Ponzano

              yes, you're right, here's my code //those 2 handle are obtained into main program HANDLE source=CreateFile("header", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL); HANDLE destination=CreateFile(filename_save, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL); bool AppendToBeginOfFile(HANDLE source, HANDLE destination) { DWORD source_size=0, destination_size=0, BRs=0, BRd=0, BW=0; char *tmp1=NULL, *tmp2=NULL; if((source == NULL) && (destination == NULL)) return false; source_size=GetFileSize(source, NULL); destination_size=GetFileSize(destination, NULL); tmp1=(char*)malloc(source_size*sizeof(char)); tmp2=(char*)malloc(destination_size*sizeof(char)); bool k=ReadFile(destination, tmp2, destination_size, &BRd, NULL); DWORD l=GetLastError(); k=ReadFile(source, tmp1, source_size, &BRs, NULL); l=GetLastError(); if((BRs < source_size) || (BRd < destination_size)) return false; SetFilePointer(destination, 0 , 0 ,FILE_BEGIN); WriteFile(destination, source, source_size, &BW, NULL); }

              B Offline
              B Offline
              Blake Miller
              wrote on last edited by
              #6

              If you are going to read and write to the destination or source files, then you need to open for both reading and writing. Also, you open the 'source' for CREATE_ALWAYS, yet you try to read from it. CREATE_ALWAYS will truncate the file to zero length, so why are you trying to read from it :wtf: If both files are supposed to exist and contain data before you start, I would do this: HANDLE source=CreateFile("header", GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); HANDLE destination=CreateFile(filename_save, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); If your destination doe snot need to exist before hand, then open it with: HANDLE destination=CreateFile(filename_save, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, NULL, NULL); Your last WriteFile call is not using arguments that make any sense. I think you wanted to append 'Destination' to end of 'Source'. // start writing at beginning of file SetFilePointer(destination, 0 , 0 ,FILE_BEGIN); // make sure file is not going ot be longer than expected SetEndOfFile(destination); // write 'source' to destination WriteFile(destination, tmp1, source_size, &BW, NULL); // now write 'destination' to destination WriteFile(destination, tmp2, destination_size, &BW, NULL); This gives you a 'destination' file with the 'source' at the beginning and the 'destination' after that.

              1 Reply Last reply
              0
              • P Paolo Ponzano

                I know, it's not possible to obtain an handle for both ? ReadFile and WriteFile?? I'll do a CloseHandle and reopen with read permission, seems the only way to me, thanks

                4 Offline
                4 Offline
                4apai
                wrote on last edited by
                #7

                hm. thats possible. why not?) CreateFile with GENERIC_READ | GENERIC_WRITE access. read about CreateFile more carefully, i'm not saying smth that is not in common-access documentation. 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.

                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