Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i tried to insert new line at the end of the file so that it will not update the file but just insert new line at the end without altering whatever is in the file.
my code is here.
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
 
int main()
{
	ofstream file("outputcheck.txt");
	ostringstream oss;
	string name="we most";
	string n="people";
	file<<name << "loves" <<n;
	return 0;
}


What I have tried:

it gives a file with that content but each time file will be updated, i mean whatever in file it goes and write just the above line. can somebody help me out.
Posted
Updated 24-Mar-16 19:48pm
Comments
Sergey Alexandrovich Kryukov 25-Mar-16 1:34am    
There is no a single new-line in your code, so what would you expect?
And "just insert new line at the end" is still a way to "update the file" (and, probably, quite pointless)...
Why?
—SA

The ofstream constructor has two parameters. The second parameter specifies the open mode and it defaults to 'out'. Change it to 'app' which stands for append.
 
Share this answer
 
Comments
[no name] 26-Mar-16 13:31pm    
I don't see a reason to give a one here (without any comment GRRRRRRR!!!). I give my small 5 to compensate this a Little bit.
Bruno
PJ Arends 26-Mar-16 19:28pm    
Thanks. I do not reply for the points, but just to give the OP a hint in what I believe is the right direction.
Please see my comments to the question. You did not even try to insert or add a new line.

C++ "object-oriented" << stream operators are so inconvenient! But perhaps you have to learn how to work with them. In this style of stream programming, there is and endl manipulator:
endl — C++ Reference[^].

Alternatively, you can simply use "\n" inside one of your strings, as shown here: Basic Input/Output - C++ Tutorials[^].

Now, there is one extremely unpleasant problem: the line separators used in files and strings are platform-dependent. This article provides a pretty comprehensive overview of the problem: Newline — Wikipedia, the free encyclopedia[^].

There are programming systems providing multiplatform facility for using this separator, that's it, they return different strings on different OS, and then you add/return this string, which is not statically defined in your code. For C++, such approach is not typical: if you write "\n", this is always a character with code point 10, and "\r" — 13, that's it.

From the other hand, more and more software products are developed to be tolerant to the kinds of line separator, they "normalize" the. Say, if you read the file as is and feed resulting string to a Windows MessageBox, the message would be shown the same way, even if you use a line separator from some other OS.

—SA
 
Share this answer
 
v2
Comments
[no name] 26-Mar-16 13:06pm    
5\r\n
Sergey Alexandrovich Kryukov 26-Mar-16 13:25pm    
Thank you.\n
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900