File api SCOL » History » Version 2
  iri, 12/17/2011 09:14 PM 
  
| 1 | 1 | iri | h1. File api SCOL  | 
|---|---|---|---|
| 2 | |||
| 3 | "http://www.scolring.org/files/doc_html/file_system.html":http://www.scolring.org/files/doc_html/file_system.html  | 
||
| 4 | |||
| 5 | +Files must be relative at the active Scol partition+.  | 
||
| 6 | There is not the close file function, Scol manage any I/O.  | 
||
| 7 | |||
| 8 | *To open a file :*  | 
||
| 9 | |||
| 10 | Read only :  | 
||
| 11 | _checkpack = fun [S] P  | 
||
| 12 | Write only :  | 
||
| 13 | _getmodifypack = fun [S] W  | 
||
| 14 | |||
| 15 | *To read a file :*  | 
||
| 16 | |||
| 17 | _getpack = fun [P] S  | 
||
| 18 | |||
| 19 | <pre>  | 
||
| 20 | fun main ()=  | 
||
| 21 | _showconsole;  | 
||
| 22 | let _checkpack "myFolder/myFile.ext" -> pFile in  | 
||
| 23 | if pFile == nil then  | 
||
| 24 | _fooS "This file doesn't exist"  | 
||
| 25 | else  | 
||
| 26 | _fooS _getpack pFile;  | 
||
| 27 | 0;;  | 
||
| 28 | </pre>  | 
||
| 29 | |||
| 30 | To get the content line by line :  | 
||
| 31 | |||
| 32 | <pre>  | 
||
| 33 | fun displayLines (list)=  | 
||
| 34 | if list == empty then  | 
||
| 35 | 0  | 
||
| 36 | else  | 
||
| 37 | let hd list -> line in  | 
||
| 38 | (  | 
||
| 39 | _fooS line;  | 
||
| 40 | displayLines tl list  | 
||
| 41 | );;  | 
||
| 42 | |||
| 43 | fun main ()=  | 
||
| 44 | _showconsole;  | 
||
| 45 | let _checkpack "myFolder/myFile.ext" -> pFile in  | 
||
| 46 | if pFile == nil then  | 
||
| 47 | (  | 
||
| 48 | _fooS "This file doesn't exist";  | 
||
| 49 | 1  | 
||
| 50 | )  | 
||
| 51 | else  | 
||
| 52 | displayLines lineextr _getpack pFile;;  | 
||
| 53 | </pre>  | 
||
| 54 | |||
| 55 | If you want the content word by word, you can do something similar with _strextr_ instead of _lineextr_.  | 
||
| 56 | |||
| 57 | To get the size :  | 
||
| 58 | |||
| 59 | _fileSize = fun [P] I  | 
||
| 60 | |||
| 61 | <pre>  | 
||
| 62 | fun main ()=  | 
||
| 63 | _showconsole;  | 
||
| 64 | let _checkpack "myFolder/myFile.ext" -> pFile in  | 
||
| 65 | _fooId  | 
||
| 66 | if pFile == nil then  | 
||
| 67 | nil  | 
||
| 68 | else  | 
||
| 69 | _fileSize pFile;  | 
||
| 70 | 0;;  | 
||
| 71 | </pre>  | 
||
| 72 | |||
| 73 | *To write in a file :*  | 
||
| 74 | |||
| 75 | _createpack = fun [S W] I  | 
||
| 76 | _appendpack = fun [S W] I  | 
||
| 77 | _storepack = fun [S S] I  | 
||
| 78 | |||
| 79 | <pre>  | 
||
| 80 | fun main ()=  | 
||
| 81 | _showconsole;  | 
||
| 82 | let _getmodifypack "anyFolder/anyFile.ext" -> wFile in  | 
||
| 83 | if 0 == _createpack "Bob and Alice " wFile then  | 
||
| 84 | if 0 == _appendpack "are married !" wFile then  | 
||
| 85 | _fooS "Done !"  | 
||
| 86 | else  | 
||
| 87 | _fooS "_appendpack : error"  | 
||
| 88 | else  | 
||
| 89 | _fooS "_createpack : error";  | 
||
| 90 | 0;;  | 
||
| 91 | </pre>  | 
||
| 92 | 2 | iri | |
| 93 | Author : iri  | 
||
| 94 | Date : december 2011  | 
||
| 95 | |||
| 96 | *Return to [[Examples]]*  |