Jump to content
  • Advertisement
Sign in to follow this  
UriKiller

1) Detecting a debugger. 2) Truncating file's end

This topic is 4678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

1) How can a program know that it is beeing run from a debugger? 2) I have n file handle (that I openned using open () with read/write permission), the file's size is fsize. How can I save only part of the file? (from the beginning to fsize-1000 for example)

Share this post


Link to post
Share on other sites
Advertisement
2) Memory recalls a function called ftrunc(), but I've never used it, so it might not be what you're looking for.

edit - my bad, that appears to be "float" truncate, not "file" truncate. But surely there is a function somewhere that lets you directly set the size of a file.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If memory serves me correctly, you just seek to the position you want to truncate and then write to the file with a zero length argument - the file will be truncated at that spot.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
If memory serves me correctly, you just seek to the position you want to truncate and then write to the file with a zero length argument - the file will be truncated at that spot.


No. The only way to truncate a file, short of using platform-specific APIs, is to just make a partial copy of it.

Share this post


Link to post
Share on other sites
On the other hand, if you're developing exclusively for Windows, you can use SetEndOfFile instead. Which seems like an altogether easier alternative then messing around with some voodoo [grin]

[EDIT] Here's an example, off the top of my head. Might want to check it your self before use just in case though. Also, keep in mind that it has no error checking, like truncating a file to longer then it is, etc. But it will give you a general idea, just in case you need it [smile]


#include <windows.h>

DWORD TruncateFile(LPCTSTR file, DWORD size)
{
// Open up the file:
HANDLE hFile = CreateFile(file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

// The open failed:
if (hFile == INVALID_HANDLE_VALUE)
return 0;

// Set the file pointer to where we want to end the file...
if (SetFilePointer(hFile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF)
{
// The move failed:
CloseHandle(hFile);
return 0;
}

// Otherwise, truncate the file!
if (!SetEndOfFile(hFile))
{
// It failed:
CloseHandle(hFile);
return 0;
}

// Close the handle and leave the function.
CloseHandle(hFile);
return 1;
}



Share this post


Link to post
Share on other sites
Quote:
No. The only way to truncate a file, short of using platform-specific APIs, is to just make a partial copy of it.

It's not that bad :) What about POSIX? (-> ftruncate, as mentioned)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!