Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   File Handling Problem (http://www.go4expert.com/forums/file-handling-problem-t8016/)

Bhullarz 28Dec2007 14:17

File Handling Problem
 
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

shabbir 28Dec2007 17:54

Re: File Handling Problem
 
use \r\n instead of \n and see what happens.

Bhullarz 28Dec2007 20:49

Re: File Handling Problem
 
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.

shabbir 28Dec2007 22:50

Re: File Handling Problem
 
Newline is not just \n but its \r\n.

Bhullarz 29Dec2007 08:09

Re: File Handling Problem
 
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.


All times are GMT +5.5. The time now is 12:26.