Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2006, 06:05
piccolo piccolo is offline
Friend
 
Join Date: Jul 2006
Posts: 28
Rept. Given: 4
Rept. Rcvd 3 Times in 1 Post
Thanks Given: 0
Thanks Rcvd at 1 Time in 1 Post
piccolo Reputation: 3
Now why would you want to use strcat?? This routine you have now is quite slow reading on a character base is too slow. Use fread and fwrite.

strcat useage is as follows:
...
char dest[20];

dest[0]='\';
strcat(dest,"this is a ");
//now dest="this is a\0"
strcat(dest,"test");
//now dest="this is a test\0"
strcat(dest,"123456789012345678901234567890");
//now you get a crash because your dest string overflows, so this is a buffer overflow.

Easier loop:
char buffer[1024];
...
while !feof(in){
nrbytes=fread(buffer,1,1024,in);
if (nrbytes) fwrite(buffer,1,nrbytes,out);
}
etc..
Backupping files can be done faster using windows functions if yer running windoze.
Reply With Quote
  #2  
Old 08-30-2006, 05:07
leosmi05 leosmi05 is offline
Friend
 
Join Date: Feb 2005
Posts: 26
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 0 Times in 0 Posts
leosmi05 Reputation: 0
Just some small remarks regarding your program.
It seems that it can not handle well an extension of 1 or 2 characters or more than 3 characters.
You should start from the back of the file name (argv[1]) and search for the last point '.' character.

And BTW, you don't need to use strcat in case you have at least 3 characters for extension. Just find the last '.' in the name and replace the next 4 characters with "BAK" and '\0'.

The other remarks from piccolo also remain.
Reply With Quote
  #3  
Old 08-30-2006, 23:23
Perch
 
Posts: n/a
Don't know if it's useful for you, but you could use the API PathRenameExtension , which is in shlwapi.dll, this API replace any extension with any extension you want:

PathRenameExtension ("file.ext", ".bak");

Hope it works for you, just #include <shlwapi.h>

This only works in windows, off course...
Reply With Quote
  #4  
Old 08-30-2006, 23:53
condzero
 
Posts: n/a
Here is a generalized routine good for your situation
and can be easily tailored or adapted to fit most
any situation:

// EX: OPEN A FILE FOR LOGGING OUTPUT IN ANALYZE MODE.
// Find the last '\\' to obtain a pointer to just the base file name part if
your buffer contains any path type info
// We could just as eaily searched for last '.' to obtain base file name
extension pointer.

Code:
char *szBuffer = buffer;
PCSTR pszBaseName = strrchr( szBuffer, '\\' );
if ( pszBaseName )  // We found a '\\', so advance to the base FILE name
{
// Increment 1 byte past our pointer
pszBaseName++;
strncpy(pszBaseName, "Asprlog.txt\0", 12); // we need 12 to include '\0'
null char	
//replace base file name with newname , here you could have appended
bak or BAK extension if you had searched on the '.' char Ex: as so: 
strncpy(pszBaseName, "bak\0", 4);
}
FILE * pFile;
pFile=fopen(szBuffer,"wt");	// open for write
... and so on, I hope you get the idea.

cheers!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



All times are GMT +8. The time now is 02:28.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( Since 1998 )