I need the program in the specified directory to put all the files in the same directory. Each folder can contain as many files with different extensions.
I have a function that moves a file to a subdirectory (Automatically creates a folder) with the same name, only with a truncated extension, here’s the actual code: (Working path = C:\\TestPath, filename = C:\\TestPath\\dasdas.txt, to = dasdas)

 void move_filename_to (const TCHAR* filename, const TCHAR* to) { const unsigned int s = sizeof(working_path) + sizeof(TEXT("\\")) + sizeof(to) + 1; TCHAR directory[s] = ""; _tcscpy(directory, working_path); _tcscat(directory, TEXT("\\")); _tcscat(directory, to); directory[s] = '\0'; //Лично здесь, все в порядке и создается директория (C:\TestPath\dasdas) //directory = C:\\TestPath\\dasdas _mkdir(directory); printf("\tSubdirectory %s created\n", directory); const unsigned int old_size = sizeof(working_path) + sizeof(TEXT("\\")) + sizeof(filename) + 1; TCHAR old_path[old_size] = ""; _tcscpy(old_path, working_path); _tcscat(old_path, TEXT("\\")); _tcscat(old_path, filename); old_path[old_size] = '\0'; //Здесь тоже все отлично //old_path = C:\\TestPath\\dasdas.txt const unsigned int new_size = sizeof(working_path) + sizeof(TEXT("\\")) + sizeof(TEXT("\\")) + sizeof(to) + sizeof(filename) + 1; TCHAR new_path[new_size] = {0,}; //На этой строчке to = dasdas _tcscpy(new_path, working_path); //А тут она уже становится нулевой :( _tcscat(new_path, TEXT("\\")); _tcscat(new_path, to); _tcscat(new_path, TEXT("\\")); _tcscat(new_path, filename); new_path[new_size] = '\0'; printf("Moving from %s to %s\n", old_path, directory); printf("DEBUG: \n\t\told_path = %s\n\t\tnew_path = %s\n\t\tdirectory = %s", old_path, new_path, directory); int status = rename(old_path, new_path); printf("\t\tMoving "); if (status == 0) printf(" OK\n"); else printf(" failed\n"); delete[] directory; delete[] old_path; delete[] new_path; } 

Why disappears to ?

to still exist to zoned

  • one
    Maybe you should add a little c++ ? - Vladimir Gamalyan
  • one
    Do not confuse sizeof and number of characters. In the case of TCHAR difference can be doubled - gecube
  • delete [] did not understand at the end either. Why did you add it there? - gecube
  • And in general - try to use protected versions of the functions for working with strings with the _s suffix (for example, _tcscpy_s ) - gecube
  • @gecube moreover, sizeof (to) will return the size of the pointer, not the string. - αλεχολυτ

0