Guest User

Untitled

a guest
Nov 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. char *new_name(char *old_name, char *operation)
  2. {
  3. char file_num[2];
  4.  
  5. char *edited_name = ( char* ) malloc( strlen( old_name ) + strlen( operation ) );
  6. strcat( edited_name, old_name );
  7. strcat( edited_name, operation );
  8.  
  9. // Iterate through names of new file to see which doesn't exist
  10. int fnum = 1;
  11. char *tempname = ( char* ) malloc( strlen( old_name ) + strlen( operation ) + sizeof( file_num ) + sizeof( ".txt" ) );
  12. do
  13. {
  14. strcpy( tempname, edited_name );
  15. sprintf( file_num, "%d", fnum++ );
  16. strcat( tempname, file_num );
  17. strcat( tempname, ".txt" );
  18. } while ( file_exists( tempname ) );
  19. free(edited_name);
  20.  
  21. return tempname;
  22. }
  23.  
  24. int main()
  25. {
  26.  
  27. char *old_name = "textfile";
  28. char *operation = "_join";
  29. char *out_name = new_name(old_name, operation);
  30.  
  31. printf( "%s", out_name );
  32.  
  33. return 0;
  34. }
Add Comment
Please, Sign In to add comment