createfile...

is there a way to open and read a file than write the same file but in a different name maybe?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    case IDB_TESTBUTTON:
                                    {
                                                   
                                                    szFileTitle[0] = '\0'; 
					 szFileName [0] = '\0'; 
if( PopFileDlg( hwnd, szFileName, szFileTitle, TRUE ) )
{
  
HANDLE	hNewFile, hOldFile;	
DWORD	dwWritten, dwRead;
	HEADER	header;	
    BYTE	*bBuffer;	
    char readBuffer[256]; 	
    
    szFileTitle[strchr( szFileTitle, '.' ) - szFileTitle] = '\0';
					// change it to fst -> first
					strcat( szFileTitle, "test.exe" );
  hOldFile = CreateFile( szFileTitle, 
											   GENERIC_READ, 
											   FILE_SHARE_READ, 
											   NULL, OPEN_ALWAYS, 
											   FILE_ATTRIBUTE_NORMAL,
											   NULL );
						// read the original file byte in the buffer
						ReadFile( hOldFile, readBuffer, dwNewSize, &dwRead, NULL );
						
						hNewFile = CreateFile( readBuffer, 
											   GENERIC_WRITE, 
											   FILE_SHARE_WRITE, 
											   NULL, CREATE_ALWAYS, 
											   FILE_ATTRIBUTE_NORMAL,
											   NULL );
						// write this buffer to the new file
						WriteFile( hNewFile, readBuffer, dwRead, &dwWritten, NULL );
										   
									




running it makes no files...
Last edited on
You use CreateFile() to open an existing file. Then use ReadFile() or ReadFileEx() to read the contents. Finally create or overwrite the destination file with a different call to CreateFile() and then use WriteFile() on it to write whatever needs writing.

Does that help?
read below...
Last edited on
here is win32 same thing but when i pressa button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    case IDB_TESTBUTTON:
                                    {
                                                   
                                                    szFileTitle[0] = '\0'; 
					 szFileName [0] = '\0'; 
if( PopFileDlg( hwnd, szFileName, szFileTitle, TRUE ) )
{
  
HANDLE	hNewFile, hOldFile;	
DWORD	dwWritten, dwRead;
	HEADER	header;	
    BYTE	*bBuffer;	
    char readBuffer[256]; 	
    
    szFileTitle[strchr( szFileTitle, '.' ) - szFileTitle] = '\0';
					// change it to fst -> first
					strcat( szFileTitle, "test.exe" );
  hOldFile = CreateFile( szFileTitle, 
											   GENERIC_READ, 
											   FILE_SHARE_READ, 
											   NULL, OPEN_ALWAYS, 
											   FILE_ATTRIBUTE_NORMAL,
											   NULL );
						// read the original file byte in the buffer
						ReadFile( hOldFile, readBuffer, dwNewSize, &dwRead, NULL );
						
						hNewFile = CreateFile( readBuffer, 
											   GENERIC_WRITE, 
											   FILE_SHARE_WRITE, 
											   NULL, CREATE_ALWAYS, 
											   FILE_ATTRIBUTE_NORMAL,
											   NULL );
						// write this buffer to the new file
						WriteFile( hNewFile, readBuffer, dwRead, &dwWritten, NULL );
										   
									



the file comes out 0 BYTES
Last edited on
The file it creates says "is not a valid win32 application"..and it is 0 bytes
closed account (DSLq5Di1)
Is the path of szFileTitle correct? you've used CreateFile() with OPEN_ALWAYS, meaning if the file doesn't exist, it will be created.. resulting in a 0 byte file.

I get the impression you only wish to copy the file, see:-
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851
Well, unless you are reading an executable file of just 256 bytes or less, your reading part is wrong. You normally read repeatedly until you reach the end of the file. Usually a single call to ReadFile() is never enough unless you are willing to store the entire file contentes in RAM, which is usually never done.

Also note that you should be calling CloseHandle() on the handles obtained via CreateFile(). You could be seeing zero bytes because of this. CloseHandle() will, among a few other things, flush data to disk.

So assuming your construction of the filename is correct (I didn't check), your problem is probably in the above. So program it like this:

1. Open the original file with CreateFile().
2. Create the destination file with CreateFile().
3. Start a while loop and loop while ReadFile() keeps returning TRUE.
4. In every loop iteration, write the buffer contents to the destination file.
5. Use CloseHanlde() to close the handles obtained in #1 and #2.
Edit: I see that sloppy9 has already mentioned this!

is there a way to open and read a file than write the same file but in a different name maybe?

If you want an exact copy, wouldn't it be better to use CopyFile() ??

CopyFile function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851%28v=vs.85%29.aspx

Or are you heading somewhere else?
Last edited on
Topic archived. No new replies allowed.