Need more examples ...

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Need more examples ...

Arie van Wingerden
When trying things out I find a huge module reference but a limited tutorial and manual.

E.g. I try to open a textfile for read.
I find:
  - Stdio.File (non-buffered)
  - Stdio.FILE (buffered)
However I cannot find a simple example of how to use it ...

I tried several ways to use the open() method, but then it says that neither Stdio.File nor Stdio.FILE have an open method, which is not conform the module reference.

Maybe I missed the obvious :-)

So, are there more examples available???
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need more examples ...

Chris Angelico
On Fri, Dec 9, 2016 at 10:33 PM, Arie van Wingerden <[hidden email]> wrote:

> When trying things out I find a huge module reference but a limited tutorial
> and manual.
>
> E.g. I try to open a textfile for read.
> I find:
>   - Stdio.File (non-buffered)
>   - Stdio.FILE (buffered)
> However I cannot find a simple example of how to use it ...
>
> I tried several ways to use the open() method, but then it says that neither
> Stdio.File nor Stdio.FILE have an open method, which is not conform the
> module reference.
>
> Maybe I missed the obvious :-)
>
> So, are there more examples available???

Normally what you'll do in Pike is to start by constructing an object.
In the case of Stdio.File and Stdio.FILE, it works like this:

> Stdio.File("spam.pike");
(1) Result: Stdio.File("spam.pike", 0, 777 /* fd=10 */)

> Stdio.File("spam.pike")->read(64);
(2) Result: "int main()\n"
            "{\n"
            "\tint x = 123;\n"
            "\twhile (string command = Stdio.stdin."

In the documentation, the constructor is create(). Once you've created
the object, you'll be able to use all the methods listed in the docs.

ChrisA

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need more examples ...

Pontus Östlund
In reply to this post by Arie van Wingerden

9 dec. 2016 kl. 12:33 skrev Arie van Wingerden <[hidden email]>:

When trying things out I find a huge module reference but a limited tutorial and manual.

E.g. I try to open a textfile for read.
I find:
  - Stdio.File (non-buffered)
  - Stdio.FILE (buffered)
However I cannot find a simple example of how to use it ...

I tried several ways to use the open() method, but then it says that neither Stdio.File nor Stdio.FILE have an open method, which is not conform the module reference.

Maybe I missed the obvious :-)

So, are there more examples available???

We could for sure be better at providing examples and tutorials. One of these days...

There are many ways to read a file:

The whole file in one sweep: 

string s = Stdio.read_file("file.ext");

And I guess this one way of using the File object.

Stdio.File f = Stdio.File("file.ext", "r");

or alternatively

Stdion.File f = Stdio.File();
f->open("file.ext", "r");

and then

string a = f->read(1024); -> read one kb
string b = f->read();     -> read to end of file or stream


Hope this works.


# Pontus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need more examples ...

Arie van Wingerden

2016-12-09 13:18 GMT+01:00 Pontus Östlund <[hidden email]>:

9 dec. 2016 kl. 12:33 skrev Arie van Wingerden <[hidden email]>:

When trying things out I find a huge module reference but a limited tutorial and manual.

E.g. I try to open a textfile for read.
I find:
  - Stdio.File (non-buffered)
  - Stdio.FILE (buffered)
However I cannot find a simple example of how to use it ...

I tried several ways to use the open() method, but then it says that neither Stdio.File nor Stdio.FILE have an open method, which is not conform the module reference.

Maybe I missed the obvious :-)

So, are there more examples available???

We could for sure be better at providing examples and tutorials. One of these days...

There are many ways to read a file:

The whole file in one sweep: 

string s = Stdio.read_file("file.ext");

And I guess this one way of using the File object.

Stdio.File f = Stdio.File("file.ext", "r");

​I used this, but I was not sure what it *meant* ...
Does this open the file?
Is this the work of a "create" method?​
 
or alternatively

Stdion.File f = Stdio.File();

​So is there a kind of default constructor at work?​

 
f->open("file.ext", "r");

and then

string a = f->read(1024); -> read one kb
string b = f->read();     -> read to end of file or stream


Hope this works.


# Pontus

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need more examples ...

Bertrand LUPART - Linkeo.com
In reply to this post by Pontus Östlund

We could for sure be better at providing examples and tutorials. One of these days...

There are many ways to read a file:

The whole file in one sweep: 

string s = Stdio.read_file("file.ext");

And I guess this one way of using the File object.

Stdio.File f = Stdio.File("file.ext", "r");

or alternatively

Stdion.File f = Stdio.File();
f->open("file.ext", "r");

and then

string a = f->read(1024); -> read one kb
string b = f->read();     -> read to end of file or stream


Hope this works.

How to read a file one line at a time :

object file = Stdio.FILE("example.txt","r");
foreach(file; int line_num; string line_data)
{
write("%O %O\n", line_num, line_data);
}


-- 
Bertrand
Loading...