C/C++ Windows API

Moderators: Lundin
Number of threads: 450
Number of posts: 1225

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Resize file problem Posted by pk_bona on 13 Sept 2008 at 12:36 PM
I’ve written the following function to resize a given binary file to a given size.
The function works when the file is located at C:\ , but it does not when the file is inside a folder.
Any idea why this is happening?

#include <windows.h>

void truncate_file (long int  new_size , char* file_name)
{

char szFileName[MAX_PATH];   //full path

GetCurrentDirectory(MAX_PATH,szFileName);
strcat(szFileName,"\\");
strcat(szFileName,file_name);


HANDLE hFile = CreateFile(szFileName,GENERIC_WRITE,
                     0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

SetFilePointer(hFile,new_size,NULL,FILE_BEGIN);

SetEndOfFile(hFile);

}



Thanks.
Report
Re: Resize file problem Posted by BitByBit_Thor on 15 Sept 2008 at 3:18 AM
: I’ve written the following function to resize a given binary file to
: a given size.
: The function works when the file is located at C:\ , but it does not
: when the file is inside a folder.
: Any idea why this is happening?
:

Probably because GetCurrentDirectory does not always return the same string. You should use a different way of determining the directory. I assume your intention is to get the EXE file directory? In that case try using GetModuleFileName (I believe it was called), and extract the path portion of the returned filename.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Resize file problem Posted by AsmGuru62 on 15 Sept 2008 at 4:09 AM
: : I’ve written the following function to resize a given binary file to
: : a given size.
: : The function works when the file is located at C:\ , but it does not
: : when the file is inside a folder.
: : Any idea why this is happening?
: :
:
: Probably because GetCurrentDirectory does not always return the same
: string. You should use a different way of determining the directory.
: I assume your intention is to get the EXE file directory? In that
: case try using GetModuleFileName (I believe it was called), and
: extract the path portion of the returned filename.
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry


Also, where is CloseHandle (hFile);?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.