endline

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

endline

 
0
  #1
Nov 7th, 2008
I am writing to a text file. Everything is working fine, except I can not figure out how to start a newline. I have tried

fprintf(txtFile, "%s", "\n");
fprintf(txtFile, "%s", '\n');
fprintf(txtFile, "%s\n", " ");

where txtFile is my FILE handle. How do I create a newline? Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: koteswarvijay is an unknown quantity at this point 
Solved Threads: 2
koteswarvijay's Avatar
koteswarvijay koteswarvijay is offline Offline
Newbie Poster

Re: endline

 
0
  #2
Nov 7th, 2008
fprintf(txtFile, "\n");
This should work.

Just to keep you informed here, use of %s is only when you need to store a variable.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: endline

 
0
  #3
Nov 7th, 2008
Can i also point out that those are C functions. If your after the C++ solution it goes something like this

  1. #include <ofstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(void){
  6. ofstream out_file;
  7. out_file.open("example.txt");
  8.  
  9. if(out_file.is_good()){
  10. out_file << "\nHello, World";
  11. out_file.close();
  12. }else{
  13. cout << "Error Opening File";
  14. cin.get();
  15. }
  16.  
  17. return 0;
  18. }

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: endline

 
0
  #4
Nov 7th, 2008
The 1st printf in the original post ( fprintf(txtFile, "%s", "\n"); ) should work too. The simplest way in C style is fputc('\n',txtFile) .

Apropos,
  1. out_file << "\nHello, World";
  2. out_file.close();
makes ill-formed text file where the last line is not terminated by line separator...
Last edited by ArkM; Nov 7th, 2008 at 1:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: endline

 
0
  #5
Nov 7th, 2008
Allow me to correct my error

  1. out_file << "\nHello, World\n";
  2. out_file.close();
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC