How to read txt file and output to display

classic Classic list List threaded Threaded
5 messages Options
sb
Reply | Threaded
Open this post in threaded view
|

How to read txt file and output to display

sb
I have a txt file which includes a help text for a program that I am trying
to write.
I want to display its contents to the display (i.e. read from txt file and
flush its content to the display) when selected from a menu.
I couldn't find the exact command to do this (if any)?
Most of the commands refer to reading data with delimiters. I want to read
from a simple txt file which includes text (any length and any number of
lines).
Is this possible?



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


Reply | Threaded
Open this post in threaded view
|

Re: How to read txt file and output to display

Uwe Damm

Am 14.03.19 um 21:30 schrieb sb:

> I have a txt file which includes a help text for a program that I am trying
> to write.
> I want to display its contents to the display (i.e. read from txt file and
> flush its content to the display) when selected from a menu.
> I couldn't find the exact command to do this (if any)?
> Most of the commands refer to reading data with delimiters. I want to read
> from a simple txt file which includes text (any length and any number of
> lines).
> Is this possible?
>
>
>
> --
> Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
>
>
>

fopen and fgetl should do the job (see > help fgetl)


fd=fopen('test.txt','r');

while(-1 != (a=fgetl(fd)))
     disp (a);
end
fclose(fd);



Reply | Threaded
Open this post in threaded view
|

Re: How to read txt file and output to display

apjanke-floss
In reply to this post by sb


On 3/14/19 4:30 PM, sb wrote:

> I have a txt file which includes a help text for a program that I am trying
> to write.
> I want to display its contents to the display (i.e. read from txt file and
> flush its content to the display) when selected from a menu.
> I couldn't find the exact command to do this (if any)?
> Most of the commands refer to reading data with delimiters. I want to read
> from a simple txt file which includes text (any length and any number of
> lines).
> Is this possible?
>

Sure. Use fopen() with fgetl() or fgets() to read the text from the
file, collect those in a cellstr, and then print them using fprintf().
The "line-oriented input" functions are the family you need to read
plain text. See https://octave.org/doc/v4.4.1/Line_002dOriented-Input.html.

Cheers,
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: How to read txt file and output to display

siko1056
In reply to this post by sb
On Thu, Mar 14, 2019 at 9:45 PM sb <[hidden email]> wrote:
I have a txt file which includes a help text for a program that I am trying
to write.
I want to display its contents to the display (i.e. read from txt file and
flush its content to the display) when selected from a menu.
I couldn't find the exact command to do this (if any)?
Most of the commands refer to reading data with delimiters. I want to read
from a simple txt file which includes text (any length and any number of
lines).
Is this possible?



>> str = fileread ("simple.txt"); 

HTH,
Kai



Reply | Threaded
Open this post in threaded view
|

Re: How to read txt file and output to display

Mike Miller-4
In reply to this post by sb
On Thu, Mar 14, 2019 at 15:30:38 -0500, sb wrote:
> I have a txt file which includes a help text for a program that I am trying
> to write.
> I want to display its contents to the display (i.e. read from txt file and
> flush its content to the display) when selected from a menu.
> I couldn't find the exact command to do this (if any)?
> Most of the commands refer to reading data with delimiters. I want to read
> from a simple txt file which includes text (any length and any number of
> lines).
> Is this possible?

Of course. The simplest solution is probably the 'type' function:

    filename = "/path/to/README.txt";
    type ("-q", filename)

Another very easy way is the 'fileread' function:

    filename = "/path/to/README.txt";
    disp (fileread (filename));

Or if you like to read the file line-by-line:

    filename = "/path/to/README.txt";
    fid = fopen (filename, "r");
    while (! feof (fid))
      s = fgets (fid);
      fputs (stdout, s);
    endwhile
    fclose (fid);

--
mike



signature.asc (849 bytes) Download Attachment