File Handling Problem

Discussion in 'C' started by Bhullarz, Dec 28, 2007.

  1. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    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 a moderator: Dec 28, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    use \r\n instead of \n and see what happens.
     
  3. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    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.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Newline is not just \n but its \r\n.
     
  5. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice