help-octave
[Top][All Lists]
Advanced

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

Re: Error while opening a .txt file


From: N
Subject: Re: Error while opening a .txt file
Date: Sun, 7 Jun 2020 15:40:12 +0200

> Am 07. Juni 2020 um 14:57 Uhr schrieb "N":
> > On Sun, 7 Jun 2020 14:46:58 +0200
> > Markus Mützel <markus.muetzel@gmx.de> wrote:
> > 
> > > Am 07. Juni 2020 um 14:37 Uhr schrieb "Andrew Janke":
> > > > Or onCleanup! It can make your code really concise.
> > > > 
> > > > file = 'data.txt';
> > > > [fid, msg] = fopen(file, 'r');
> > > > if fid == -1
> > > >   error('Failed opening file ''%s'': %s', file, msg);
> > > > end
> > > > RAII.fid = onCleanup(@() fclose(fid));  % automatically gets called when
> > > > RAII is cleared
> > > > data = textscan (fid, '% f', 'HeaderLines', 5);
> > > > period_arr = data {1};
> > > > % and that's it! no try/catch needed
> > > > 
> > > > Cheers,
> > > > Andrew
> > > > 
> > > 
> > > Or that.
> > > But keep in mind that this might not work in scripts unless you 
> > > explicitly clear the object returned by onCleanup when you are done.
> > > 
> > > Markus
> > 
> > If it does not get automatically cleared then function exit this must be a 
> > problem. Or?
> > 
> 
> I don't understand what you are trying to say.
> "onCleanup" always works "as expected" in functions.

That's perfect.

> When used in scripts, the object returned by it might not get out of scope 
> when the script finishes executing. Hence you might need to explicitly clear 
> the onCleanup variable.

Then it must be handled manually for a .m file without function declaration.

> > Think files are closed automatically in C but not in Octave if I got it 
> > right.
> > 
> I don't know what you are trying to say with that either. I can't even 
> guess... :-)

If files are not closed automatically problems might pile up in case program 
does not close them.



reply via email to

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