File Handling Problem

Bhullarz's Avatar
Skilled contributor
Hi ! I am using C++ to create a new .inf file but problem I am facing that I am not able to enter the 2nd line in new line.

code is :
Code:
#include<iostream.h>
#include<fstream.h>
void main()
{
ofstream newfile;
newfile.open("c:\example.inf")
newfile<<"First Line \n";
newfile<<"Second Line \n";
newfile.close()
}
output in file is:
First LineSecond Line

Kindly rectify me here

Last edited by shabbir; 28Dec2007 at 17:54.. Reason: Code block
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
use \r\n instead of \n and see what happens.
0
Bhullarz's Avatar
Skilled contributor
Quote:
Originally Posted by shabbir
use \r\n instead of \n and see what happens.
Sir ! I tried this code. but It removes the First character of First Line.
Source is:
Code:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
void main()
{
	char sample[1000];
	ofstream myfile;
	myfile.open("c:/example.txt");
	myfile<<"Writing in the file.";
	myfile<<"\r \n Next line";
	cout<<"Done";
}
Output in file is :
Code:
riting in the file.
Next Line
But I don't understand how did it happen? Like if I tell what i understands from this code.
I take example of notepad here. Like I opened a text file in notepad. I typed "Writing in the file" then I press HOME key on key board which is I think equivalent to \r(Carriage Return).
Then I type Next Line. Now the Output would be here as follows :
Code:
Next LineWriting in the file.
Kindly make me understand.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Newline is not just \n but its \r\n.
0
Bhullarz's Avatar
Skilled contributor
Quote:
Originally Posted by shabbir
Newline is not just \n but its \r\n.
Thank you Shabbir. It worked.I found my error. Actually i was giving a space between /r and /n which was replacing first character. Thanks Once again.