943,971 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2125
  • C RSS
Oct 14th, 2009
0

fprintf doesn't add newline to file in cygwin

Expand Post »
Hi.
I have this codeline:
  1. fprintf(file, "The\necake\nis\na\nlie");
When I compile and run in Linux it outputs nicely like so:
The
cake
is
a
lie

but when I compile and run in Windows (Vista), using cygwin, the newline doesn't work and output is like this:
Thecakeisalie

Anybody have an idea why?
Last edited by siggivara; Oct 14th, 2009 at 8:20 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
siggivara is offline Offline
15 posts
since Apr 2009
Oct 14th, 2009
0
Re: fprintf doesn't add newline to file in cygwin
That's curious.
Try doing \r\n instead. Not sure if that'll help, to be honest, but worth a try.
Can you get a hex editor and look at the hex of the file and see if the \n character is there? It could just be your editor.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 14th, 2009
1
Re: fprintf doesn't add newline to file in cygwin
Even more peculiar is have the E in 'ecake' disappears, what a mystery!
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Oct 14th, 2009
0
Re: fprintf doesn't add newline to file in cygwin
Click to Expand / Collapse  Quote originally posted by twomers ...
That's curious.
Try doing \r\n instead. Not sure if that'll help, to be honest, but worth a try.
Can you get a hex editor and look at the hex of the file and see if the \n character is there? It could just be your editor.
I have no idea why, but it worked like a charm. Thanks alot
Now:
The
cake
is
a
lie



If it matters I'm using eclipse c/c++ editor.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
siggivara is offline Offline
15 posts
since Apr 2009
Oct 15th, 2009
0
Re: fprintf doesn't add newline to file in cygwin
Linux uses '\n' for line breaks but Windows uses "\r\n". That should not matter because the compiler should recognize and convert to and from system specific representations. It can make a difference if you open the file as binary because binary mode will disable the text conversions.

I have not used Cygwin, but it might also be because it is a compatibility layer and if there is a disconnect somewhere it might not be converting line breaks the way it should.
Last edited by Tom Gunn; Oct 15th, 2009 at 9:16 am.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: endless loop
Next Thread in C Forum Timeline: Hi , I want to know, how to program this in C.





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


Follow us on Twitter


© 2011 DaniWeb® LLC