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