Unable to create .txt file in C++ after string manipulation

72 Views Asked by At

I am writing a program where I am expecting a symbol at the end of the string. I try to remove the symbol and adjust the string. Then I am trying to use that string as a filename to create a file. Before that, I add .txt to it as an extension. I have tried outputting the filename after that and it is a printed file. when I try to create a file the file is created and data is written on it fine. But the extension is not .txt. It's simply a "File". I can't figure out why it is not .txt. I think the problem might be related to setting '\0' manually. but if it is, then how the string concatenation is working fine?. please guide me through. Here is the code:

 string str;
        cin >> str;
        if(str[str.size() - 1] == ',')
        {
            str[str.size() - 1] = '\0';
        }
        str+=".txt";
        ofstream file;
        file.open(str);
        file<<"Hello World";
        file.close();
1

There are 1 best solutions below

2
Vlad from Moscow On BEST ANSWER

Changing the character ',' to '\0' does not change the length of the string str

if(str[str.size() - 1] == ',')
{
    str[str.size() - 1] = '\0';
}

and the member function append used implicitly in this statement

str+=".txt";

appends the character literal ".txt" to the preceding character '\0' keeping it as is.

However in the call of the method open

file.open(str);

the string is read as a C-string until the terminating zero character '\0' is encountered.

Instead you could write for example

    if( str.back() == ',')
    {
        str.pop_back();
    }
    str += ".txt";

removing the last character from the string.

Here is a demonstration program that shows the difference between the above shown approaches.

#include <iostream>
#include <string>

int main()
{
    std::string str = "File,";

    if (str[str.size() - 1] == ',') str[str.size() - 1] = '\0';

    str += ".txt";

    std::cout << "str.length() = " << str.length() << '\n';

    str = "File,";

    if (str.back() == ',') str.pop_back();

    str += ".txt";

    std::cout << "str.length() = " << str.length() << '\n';
}

The program output is

str.length() = 9
str.length() = 8

Pay attention to that the character '\0' is not outputed if you will try to output the string in the first approach.