CreateFile for reading/writing file
-
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 ad183 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 -
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 ad183 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 Paoloas 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.
-
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.
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); }
-
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); }
-
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.
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
-
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); }
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.
-
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
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.