help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Append messages to text, starting with a new line


From: Andrew Janke
Subject: Re: Append messages to text, starting with a new line
Date: Thu, 14 Nov 2019 02:55:52 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.1

fputs() just outputs whatever you give to it. It does not add a newline.
In text files or streams, a newline is an explicit character that you
must send to the output if you want line breaks in your results.

A LF character, which usually is a newline, is represented by "\n" in
Octave strings.

So do this:

fputs(fid,sprintf('iteration "%d" is successful\n',i));

That will put a newline after every message you're printing.
Cheers,
Andrew


On 11/14/19 2:52 AM, zs1992 wrote:
> Hi everyone, 
> I'd like to append messages to an existing text file within a loop, and each
> message starts with a new line.
> It seems the command "fputs" has only two input, fid and string, and every
> message is appended right after the previous one. So is there any good
> solution to it?
> 
> Here's part of my program.
> logfile_name='log_msg.txt';
> fid=fopen(logfile_name,"a");
> fclose(fid);
> ......
> for i=1:10
> ......
> fid=fopen(logfile_name,"a");
> fputs(fid,sprintf('iteration "%d" is successful',i));
> fclose(fid);
> endfor
> 
> the result is:
> iteration "1" is successfuliteration "2" is successfuliteration "3" is
> successful......
> 
> What I expected is:
> iteration "1" is successful
> iteration "2" is successful
> iteration "3" is successful
> 
> 
> 
> 
> 
> --
> Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
> 
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]