Alex Rivera | Logout

Is there another way to truncate a string?

Asked 2010-08-31T23:19:23.373
9
char out_file_name[30];  


ogSize = strlen(FileName); //i.e. - FileName_anylength.log (always.log)
ogSize -= strlen(IN_FILE_SUFFIX); //amount of chars before .log

strncpy( out_file_name, FileName, ogSize ); //out_file_name now is FileName_anylength (without the .log)  

Is this the best way to do this?

Also, how do I guard that ogSize doesn't happen to be more than 30 chars as it is coming from user input?

Thank You.

Edit
Report

1 Answer

2

Give this a look:

char *trunc;
char *outfile;

strdup( outfile, FileName );

if ( ((trunc = strstr( out_file_name, ".log" )) != NULL )
 *trunc = '\0';

return ( outfile );  // assumes returning result from function
answered 2010-08-31T23:38:11.180

Your Answer