943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1304
  • C++ RSS
Nov 7th, 2008
0

endline

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Nemoticchigga is offline Offline
98 posts
since Feb 2008
Nov 7th, 2008
0

Re: endline

fprintf(txtFile, "\n");
This should work.

Just to keep you informed here, use of %s is only when you need to store a variable.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
koteswarvijay is offline Offline
8 posts
since Sep 2008
Nov 7th, 2008
0

Re: endline

Can i also point out that those are C functions. If your after the C++ solution it goes something like this

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 7th, 2008
0

Re: endline

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,
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 7th, 2008
0

Re: endline

Allow me to correct my error

C++ Syntax (Toggle Plain Text)
  1. out_file << "\nHello, World\n";
  2. out_file.close();
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trouble compiling static-linking library
Next Thread in C++ Forum Timeline: hellow





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC