I want to save the information to a . txt in c++

Asked

Viewed 4,889 times

0

The problem here is that it creates the file 'test.txt' however when it comes to recording the file it does nothing... I’ve seen the file permission and this ok... I didn’t understand anyone could help me?

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    int numero; 
    string name; 
    ofstream outFile;

    outFile.open("c:\\x\\test.txt");
    if (! outFile) 
    {
        cout << "erro test.txt" << endl; 
        abort(); 
    } 

    cout << "Name: \n" 
        << "Fim de arquivo (Ctrl-Z) termina a entrada de dados\n\n? "; 
    while(cin >> numero >> name){ 
        outFile << numero << " " << name<< '\n'; // << Chega aqui ele não faz nada
        cout << "? "; 
    } 
    outFile.close();
    return 0;
} 
  • No Cout tries so || outfile << (number) << " << (name)<< " n"; ||

  • 1

    Probably the OS is not flush to the file. Swap '\n' for endl and make sure it resolves.

  • of ero when I try (outfile << (number) << " << (name)<< " n";)

  • edit the question and post the error

2 answers

2


The problem with your code is that the operating system is not flush the data to the file due to CTRL+Z.

To fix this problem, just replace the '\n' for endl:

outFile << numero << " " << name << endl;

0

int main()
{
    std::string input;
    std::cin >> input;
    std::ofstream out("output.txt");
    out << input;
    out.close();
    return 0;
}

-------------------------------------------

std::ofstream file;
file.open("filename");
std::string my_string = "Oi qual o seu nome?\n";
file << my_string;
file.close();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.