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: zs1992
Subject: Re: Append messages to text, starting with a new line
Date: Thu, 14 Nov 2019 02:10:32 -0600 (CST)

Andrew Janke-2 wrote
> 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
>> 
>>

Hi Andrew,
Thank you for your quick reply and it works perfectly!
Best regards,
zs



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



reply via email to

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