lundi 2 août 2021

C++ Error in creating the file (name based on other file name)

I have a problem with creating the file, which name is a little bit modified of some other name.

But if the name is not based on other name - program works - but I need to use that first method.

I used backslash ("\b") to remove ".str" from original name to modified the copy - in this program I cannot modify path_file_1 and path_file_2 so I cannot just missed the extention and add it later when I finished preparing the copy.

Here's Code:

#include <iostream>
#include <fstream>

int main()
{
    std::cout << "Starrting!" << std::endl;

    std::string path_file_1 = "Sub-File1.srt";
    std::string path_file_2 = "Sub-File2.srt";
    std::string path_file_new = path_file_1 + "\b\b\b\b-corr.srt";
    
    //std::string path_file_new = "Sub-File1-corr.srt";
    //this works!

    std::cout << path_file_1 << std::endl;
    std::cout << path_file_new << std::endl;


    //Creating a new file
    std::fstream file;

    file.open(path_file_new, std::ios::out | std::ios::binary);
    if (!file)
    {
        std::cout << "Error in creating file!!!";
        return 0;
    }
    std::cout << "File created successfully." << std::endl;
    file.close();
}

Here's Output:

Starrting!
Sub-File1.srt
Sub-File1-corr.srt
Error in creating file!!!

Aucun commentaire:

Enregistrer un commentaire