the task:

It is required to write in C ++ a program for entering text information from the keyboard into a file on the C drive:

the program opens writes the previously entered text. But I need to enter my text into the console from the keyboard and then save to the C drive:

#include <iostream> using namespace std; int main() { setlocale(0,""); // включаем кириллицу в консоли char * fileName = "C:\\example.txt"; // Путь к файлу для записи FILE * file = fopen(fileName, "w"); if (file) // если есть доступ к файлу, { char * str = "I Like The Coding!"; // инициализируем строку bool result = fputs(str, file); // и записываем ее в файл if (!result) // если запись произошла успешно cout << "Строка в файл успешно записана!" << endl; // выводим сообщение } else cout << "Нет доступа к файлу!" << endl; fclose(file); return 0; } 

Closed due to the fact that the essence of the question is incomprehensible by the participants Vladimir Martianov , iluxa1810 , Nicolas Chabanovsky 14 Nov '16 at 5:35 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    There are several ways to read lines from the input stream. If you write a program using the tools of the C language, you can read data from the console using the fgets function.

    Here is a simple demo program.

     #include <cstdio> int main() { char s[BUFSIZ]; while ( std::fgets( s, sizeof( s ), stdin ) ) { fputs( s, stdout ); } return 0; } 

    If you write a program using C ++ tools, then it is best to use the std::string class and the corresponding function std::getline . Here is a demonstration program for this approach.

     #include <iostream> #include <string> int main() { std::string s; while ( std::getline( std::cin, s ) ) { std::cout << s << std::endl; } return 0; } 

    In both programs, instead of outputting lines to the console, you will need to send them to a file.

       #include <iostream> #include <Windows.h> #include <fstream> #include <string> #include <conio.h> using namespace std; int main() { //Настройка ввода данных в консоль. SetConsoleCP(1251); SetConsoleOutputCP(1251); //Создание массивов и настройка вывода в файл. ofstream fout("text.txt"); const int n = 1000; char str[n]; gets_s(str); cout << str << endl; fout << str << endl; fout.close(); _getch(); return 0; } 

      You can try it. Already writing the result to a file. This is an example.