Вообщем, нужно записать в файл строку без пробелов, собственно я и использую для этого getline. Но у меня выходит так, что на 24 строке getline просто скипается и по итогу мне не предлагает ввести текст с клавиатуры. Код на С++ #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string path = "C:\\Users\\HP\\Desktop\\myFile.txt"; fstream fs; fs.open(path, fstream::in | fstream::out | fstream::app); if (!fs.is_open()) { cout << "Error opened !" << endl; } else { int num; string msg; cout << "File opened !" << endl; cout << "Write 1 for write message in file " << endl; cout << "Write 2 for right all messages in file " << endl; cin >> num; if (num == 1) { cout << "Write message: "; getline(cin, msg); fs << msg << "\n"; } if (num == 2) { while (!fs.eof()) { msg = ""; getline(fs, msg); cout << msg << endl; } } } fs.close(); return 0; } C #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string path = "C:\\Users\\HP\\Desktop\\myFile.txt"; fstream fs; fs.open(path, fstream::in | fstream::out | fstream::app); if (!fs.is_open()) { cout << "Error opened !" << endl; } else { int num; string msg; cout << "File opened !" << endl; cout << "Write 1 for write message in file " << endl; cout << "Write 2 for right all messages in file " << endl; cin >> num; if (num == 1) { cout << "Write message: "; getline(cin, msg); fs << msg << "\n"; } if (num == 2) { while (!fs.eof()) { msg = ""; getline(fs, msg); cout << msg << endl; } } } fs.close(); return 0; }
Проблема здесь заключается в том, что после ввода числа с помощью cin, символ новой строки (\n) остается во входном буфере. Решение: cin.ignore(); // Добавить эту строку перед вызовом getline getline(cin, message);