Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
'\n' not working...

Subject: '\n' not working...

From: Masood Sadiq

Date: 29 Jan, 2009 17:03:02

Message: 1 of 7

Hi
I don't understand this...I open a file, I can successfully write to it, but each time I write to the file, everything I write just keeps getting appended to the one and only line in the file. I want each write to appear one underneath the next

I tried to add a 'return' character but to no avail. Any ideas what I'm doing wrong?
--------------
fid = fopen('Log.txt','a');
fprintf(fid, Parameter:%s must be less than 29 characters\n', ParamName);
fclose(fid);
---------------

Cheers
Masood

Subject: '\n' not working...

From: checkmate

Date: 29 Jan, 2009 17:38:07

Message: 2 of 7

On Jan 29, 9:03=A0am, "Masood Sadiq" <masood.sa...@power.alstom.com>
wrote:
> Hi
> I don't understand this...I open a file, I can successfully write to it, =
but each time I write to the file, everything I write just keeps getting ap=
pended to the one and only line in the file. I want each write to appear on=
e underneath the next
>
> I tried to add a 'return' character but to no avail. Any ideas what I'm d=
oing wrong?
> --------------
> fid =3D fopen('Log.txt','a');
> fprintf(fid, Parameter:%s must be less than 29 characters\n', ParamName);=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> fclose(fid);
> ---------------
>
> Cheers
> Masood

The '\n' character may be already in there, but due to different
operating system conventions, it may not show up on your text editor
when you view them after the text file has been written. Hope I'm
saying this right and that it helps.

Subject: '\n' not working...

From: Steven Lord

Date: 29 Jan, 2009 17:48:11

Message: 3 of 7


"Masood Sadiq" <masood.sadiq@power.alstom.com> wrote in message
news:glsng6$6j8$1@fred.mathworks.com...
> Hi
> I don't understand this...I open a file, I can successfully write to it,
> but each time I write to the file, everything I write just keeps getting
> appended to the one and only line in the file. I want each write to appear
> one underneath the next
>
> I tried to add a 'return' character but to no avail. Any ideas what I'm
> doing wrong?
> --------------
> fid = fopen('Log.txt','a');

If you're writing to a text file, open the file in text mode.

fid = fopen('Log.txt', 'at');

See the Remarks section of the reference page for FPRINTF (which talks about
FOPENing files in text mode):

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html?BB=1

--
Steve Lord
slord@mathworks.com

Subject: '\n' not working...

From: phil invl**

Date: 29 Jan, 2009 18:30:04

Message: 4 of 7

Could it be that you need a \r as well (or instead though inliiely) for your os to recognize it?

Subject: '\n' not working...

From: checkmate

Date: 29 Jan, 2009 21:27:09

Message: 5 of 7

On Jan 29, 10:30=A0am, "phil invl**" <73...@mt2009.com> wrote:
> Could it be that you need a \r as well (or instead though inliiely) for y=
our os to recognize it?

In other words, there could be differences between operating systems
(POSIX or Windows DOS) on carriage return ('\r') or new line ('\n')
character.

Subject: '\n' not working...

From: Masood Sadiq

Date: 30 Jan, 2009 11:11:01

Message: 6 of 7


> If you're writing to a text file, open the file in text mode.
>
> fid = fopen('Log.txt', 'at');
>
> See the Remarks section of the reference page for FPRINTF (which talks about
> FOPENing files in text mode):
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html?BB=1
>
> --
> Steve Lord
> slord@mathworks.com
>
--------------------------
Fantastic. Changing the 'fopen' command to 'at' did the trick.
Thanks very much!

Subject: '\n' not working...

From: asma asmaa

Date: 2 Jul, 2012 08:45:07

Message: 7 of 7

"Steven Lord" <slord@mathworks.com> wrote in message <glsq4r$5d$1@fred.mathworks.com>...
>
> "Masood Sadiq" <masood.sadiq@power.alstom.com> wrote in message
> news:glsng6$6j8$1@fred.mathworks.com...
> > Hi
> > I don't understand this...I open a file, I can successfully write to it,
> > but each time I write to the file, everything I write just keeps getting
> > appended to the one and only line in the file. I want each write to appear
> > one underneath the next
> >
> > I tried to add a 'return' character but to no avail. Any ideas what I'm
> > doing wrong?
> > --------------
> > fid = fopen('Log.txt','a');
>
> If you're writing to a text file, open the file in text mode.
>
> fid = fopen('Log.txt', 'at');
>
> See the Remarks section of the reference page for FPRINTF (which talks about
> FOPENing files in text mode):
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html?BB=1
>
> --
> Steve Lord
> slord@mathworks.com
>

THANK YOU VERY MUCH
IT'S WORKING NOW
I HAVE TRIED fopen with 'wt' in order to discard the existing content.
thank you

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us