Answer the question
In order to leave comments, you need to log in
Text is entered incorrectly in a text document, how to fix it?
I have code that uses fstream
. Everything is written to a file "file.txt"
.
When I enter 2 or more words, then in the file "file.txt"
each word is on its own line, no matter how I try to fix it.
This is what this piece of code looks like
string WritingToFile, path = "file.txt";
fstream out;
cout << "Пиши все что хочешь\n";
while (WritingToFile != "Конец записи") {
ofstream out(path , ios::app);
SetConsoleCP(1251);
cin >> WritingToFile;
out << WritingToFile << endl;
SetConsoleCP(866);
}
out.close();
Answer the question
In order to leave comments, you need to log in
Use the getline function, because when you enter into the input stream using the << operator, the input occurs up to the first whitespace character. Read how I/O streams work, what are the functions to access them.
Also set a check to open the file and a check to not write an "exit" condition to the file, as in the code example below.
string WritingToFile, path = "file.txt";
ofstream out(path , ios::app);
if(out.is_open()) {
cout << "Write whatever you want\n";
while (WritingToFile != "exit") {
SetConsoleCP(1251);
getline(cin, WritingToFile);
if(WritingToFile != "exit")
out << WritingToFile << endl;
SetConsoleCP(866);
}
}
out.close();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question