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();
Changing the character
','to'\0'does not change the length of the stringstrand the member function
appendused implicitly in this statementappends the character literal
".txt"to the preceding character'\0'keeping it as is.However in the call of the method
openthe string is read as a C-string until the terminating zero character
'\0'is encountered.Instead you could write for example
removing the last character from the string.
Here is a demonstration program that shows the difference between the above shown approaches.
The program output is
Pay attention to that the character
'\0'is not outputed if you will try to output the string in the first approach.