Why don't you read me the line break in C++?

I don't know why I don't read the line break when I save the names to the file. Thank you 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;
}
 0
Author: José T., 2016-11-06

2 answers

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

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

Also, endl usually involves a fflush type call that brings absolutely nothing to your code.

A greeting.

 3
Author: eferion, 2016-11-06 21:14:14

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;
}
 0
Author: MauryCortes, 2016-11-17 06:14:18