Why does not the line feed in C ++ read me?

0

I do not know why I do not read the line break when I save the names to the file. Thanks in advance, who can help me. (I'm using codeblocks)

    #include <iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream modificar;
    modificar.open("Documento1.txt",ios::binary);
    char nombrea[10],nombreb[10],nombrec[10];
    cout<<"Ingrese tres nombres"<<endl;
    cin.getline(nombrea,10);
    cin.getline(nombreb,10);
    cin.getline(nombrec,10);
    modificar<<nombrea<<endl<<nombreb<<endl<<nombrec<<endl;
    modificar.close();
    ifstream leer;
    leer.open("Documento1.txt");
    char linea[40];
    leer>>linea;
    cout<<linea;
    leer.close();
    return 0;
}
    
asked by José T. 06.11.2016 в 19:55
source

2 answers

3

What you have to do is replace the occurrences of endl with "\r\n" .

endl is being translated by '\n' and the editor you use will surely understand that the line break occurs before the "\r\n" sequence.

In addition, endl usually entails a call type fflush that does not contribute anything to your code.

Greetings.

    
answered by 06.11.2016 / 22:14
source
0

Problem solved:

#include <iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream modificar;
    modificar.open("Documento1.txt",ios::binary);
    char nombrea[10],nombreb[10],nombrec[10];
    cout<<"Ingrese tres nombres"<<endl;
    cin.getline(nombrea,10);
    cin.getline(nombreb,10);
    cin.getline(nombrec,10);
    modificar<<nombrea<<"\r\n"<<nombreb<<"\r\n"<<nombrec<<"\r\n";
    modificar.close();
    ifstream leer;
    leer.open("Documento1.txt");
    char linea[40];
    leer>>linea;
    cout<<linea;
    leer.close();
    return 0;
}
    
answered by 17.11.2016 в 07:14