• Advertisement
Sign in to follow this  

problem with txt files and c++

This topic is 3229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! How do you make an enter in a txt fil from c++? What i mean is that it dosn't look like this: 123456789 instead it should look something like this: 1 2 3 4 . . the cod i'm using is following: fil << "1" << "2" << "3"...... i have tried to put both endl and \n after each sign but it dosen't work. help please!

Share this post


Link to post
Share on other sites
Advertisement
I got it using both endl and \n:


#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ofstream outFile;

outFile.open("test.txt");

outFile << "1\n";
outFile << "2" << endl;
outFile << "3" << endl;
outFile << "4" << endl;
outFile << "5" << endl;
outFile << "6" << endl;
outFile << "7" << endl;
outFile << "8" << endl;
outFile << "9" << endl;

outFile.close();
}





Outputs:
1
2
3
...
on my computer.

Share this post


Link to post
Share on other sites
hello!
works now, had some cod in that wasn't supposed to be their, thanks for you help either way.

Share this post


Link to post
Share on other sites
Bear in mind that the Windows newline sequence is CRLF; Notepad doesn't handle LF on its own and so will display everything on a single line.

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
Bear in mind that the Windows newline sequence is CRLF; Notepad doesn't handle LF on its own and so will display everything on a single line.

C++ streams will do newline conversion for you, unless opened in binary mode.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement