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

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

## 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 on other sites
In Windows you can use IsDebuggerPresent() to detect a debugger.

##### Share on other sites
Thanks that was quick!

##### Share on other sites
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 on other sites
Name of that function is ftruncate. So you nearly nailed it, Wyrframe. [smile]

##### Share on other sites
Hmm, my VC++ 6.0 doesn't recognize that function, any other ideas?

##### Share on other sites
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 on other sites
Quote:
 Original post by Anonymous PosterIf 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 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 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)

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632648
• Total Posts
3007642
• ### Who's Online (See full list)

There are no registered users currently online

×