473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can't newlines to files

2
Hi.
This is my code:

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
ofstream file ("afile.txt");

file << "This is a line.\n";
file << "This is another line.\n";

return 0;
}

This is what the contents of afile.txt look like:

This is a line.This is another line.

Does anyone know why the new lines aren't being written?

Thanks for any help.
Sep 2 '07 #1
2 1125
JosAH
11,448 Expert 8TB
Let the iostream library figure out how to write a newline:

Expand|Select|Wrap|Line Numbers
  1. file << "This is a line." << endl;
  2. file << "This is another line." << endl; 
  3.  
Despite the fact we're working with computers for more than fifty years now,
OS writers still can't agree which characters, in what combination, make up
a proper new line in a text file. So, the library creators took the burden away
from you so you don't have to use platform dependent constructs in your own
code; it is hidden in the library implementation.

kind regards,

Jos
Sep 2 '07 #2
johnj
2
I've tried using endl's and I still get the same output.

But I discovered this. When I copy and paste the the contents of afile.txt into like a Word document the new line shows up. It looks like this:

This is a line.
This is another line.

So I compiled the code again without newlines or endl's and when I copy and paste that output into a Word document the newlines aren't there, as they shouldn't be. It looks like this:

This is a line.This is another line.

So it seems like there's some reason specific to notepad (the program I'm using to create the .txt files) that will allow the newlines to be written but not displayed. Is this a product of the reason you gave in your last post or something different?

Again, thanks for your time.
Sep 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alessandro Crugnola *sephiroth* | last post by:
Hi, I'm trying to detect the newlines method of a file opened. I'm using the 'U' mode as parameter when opening a file, but for every file opened the result is always "\r\n", even if the file has...
0
by: skip | last post by:
*argh!* I hate XML! There, now that that's off my chest... I am trying to save Python code as attributes of an XML tag with xml.dom.minidom machinery. The code, predicatbly enough, contains...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
2
by: Fred Dag | last post by:
Hi, When writing using both these methods ( StreamWriter.Write("\n") vs WriteLine() ) to a file and open in Notepad I see a special character instead of newlines with Write("\n") vs a newline...
1
by: HopfZ | last post by:
Not a question. I tested how two kinds of newlines (\n and \r\n) interact with three browsers: Fx (Firefox 2), Op (Opera 9), Ie (IE 7) (all three on Windows XP). Result: The string...
5
by: lister | last post by:
Hi, I am trying to output a newline, but this doesn't work: echo "Line1\nLine2"; Before anyone says, I know HTML doesn't recognise newlines. I don't need newlines in the rendered HTML, I...
4
by: -Lost | last post by:
For example: var newlines = 'a\n\nb\n\nc'; alert(newlines); Yet, if I get that *exact* same line from an XMLHttpRequest's responseText, it is always alerted as: a\n\nb\n\nc
2
by: =?Utf-8?B?QmFzIFJpam5pZXJzY2U=?= | last post by:
Hello, I'm trying to write: strMulti = new string { @"c:\pagefile.sys", "512", "512" }; with: keyRead.SetValue(regKey, strMulti, regKind); Where reKind is Multistring Unfortunately it ends...
14
by: Rob | last post by:
I am trying to perform client-side input validation for a textarea to determine that the number of characters doesn't exceed a certain length. Currently, I am just using str.length, but if the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.