First question

Suppose there is a function Func() . How can I implement the creation of a separate out text file with the name имя_Ρ„Π°ΠΉΠ»Π°.txt using streams with each function call?

 ofstream out; out.open("имя_Ρ„Π°ΠΉΠ»Π°.txt"); 

but so that a number (sequence number) is added to the file name each time, like имя_Ρ„Π°ΠΉΠ»Π°_1.txt on the first call to the function, имя_Ρ„Π°ΠΉΠ»Π°_2.txt on the second call.

Second question

The user enters the path to the file, for example, D:\Π”ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Ρ‹\input.txt . The program reads this, but when you try to open such a file along this path, an error appears as if the file is not open.

 cout << "Type full path to the file" << endl; gets_s(path); ifstream dump; dump.open(path); if (!dump.is_open()) cout << "File is not opened" << endl; 

How to fix this error?

    1 answer 1

    The function to_string can help in solving the first question . In addition, you should take advantage of the opportunity, as a static variable in the function . Thus, to create a file with the name имя_Ρ„Π°ΠΉΠ»Π°_<i>.txt you need to write:

     void func() { // это статичСская пСрСмСнная // ΠΎΠ½Π° ΠΈΠΌΠ΅Π΅Ρ‚ Ρ‚ΠΈΠΏ atomic_*, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€ΠΈ Π²Ρ‹Π·ΠΎΠ²Π΅ нашСй Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΈΠ· Ρ€Π°Π·Π½Ρ‹Ρ… ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ² Π½Π΅ ΠΏΠΎΠ»ΡƒΡ‡Π°Π»ΠΈΡΡŒ Ρ„Π°ΠΉΠ»Ρ‹ с ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²Ρ‹ΠΌΠΈ ΠΈΠΌΠ΅Π½Π°ΠΌΠΈ static atomic_size_t numberFiles; size_t currentFileIndex = numberFiles++; ofstream out("имя_Ρ„Π°ΠΉΠ»Π°" + to_string(currentFileIndex) + ".txt"); ... } 

    The second question is not very clear. If the user has entered the file name, but there is no such file, then the user is mistaken with the file name. You can tell him about it, like this:

     while (!dump.is_open()) { cout << "Π’Π°ΠΊΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° Π½Π΅ сущСствуСт" << endl; cout << "ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π²Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎΠ΅ имя Ρ„Π°ΠΉΠ»Π°:" << endl; getline(cin, path); dump.open(path); } 
    • Thanks for the answer. With the second figured out, the path to the file was registered in Cyrillic and the compiler did not perceive it. Added at the beginning of the file: #include <windows.h> and in the code: SetConsoleOutputCP(1251); SetConsoleCP(1251); SetConsoleOutputCP(1251); SetConsoleCP(1251); - Nikita. L