Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char *new_name(char *old_name, char *operation)
- {
- char file_num[2];
- char *edited_name = ( char* ) malloc( strlen( old_name ) + strlen( operation ) );
- strcat( edited_name, old_name );
- strcat( edited_name, operation );
- // Iterate through names of new file to see which doesn't exist
- int fnum = 1;
- char *tempname = ( char* ) malloc( strlen( old_name ) + strlen( operation ) + sizeof( file_num ) + sizeof( ".txt" ) );
- do
- {
- strcpy( tempname, edited_name );
- sprintf( file_num, "%d", fnum++ );
- strcat( tempname, file_num );
- strcat( tempname, ".txt" );
- } while ( file_exists( tempname ) );
- free(edited_name);
- return tempname;
- }
- int main()
- {
- char *old_name = "textfile";
- char *operation = "_join";
- char *out_name = new_name(old_name, operation);
- printf( "%s", out_name );
- return 0;
- }
Add Comment
Please, Sign In to add comment