Difference between revisions of "Working with Files and File Systems in Recital"

From Recital Documentation Wiki
Jump to: navigation, search
(Working with Files and FileSystems in Recital)
(Miscellaneous File and File System Commands and Functions)
 
(32 intermediate revisions by one user not shown)
Line 1: Line 1:
 
==Working with Files and File Systems in Recital==
 
==Working with Files and File Systems in Recital==
===Opening and Creating Text Files in Recital===
+
===Handling Text Files in Recital===
* [[FCREATE()|fcreate()]]
+
Recital includes functions allowing text files to be created, opened and closed and their contents to be read, overwritten or appended to.
  
* [[FOPEN()|fopen()]]
+
====Opening and Creating Text Files in Recital====
 +
* [[FCREATE()|fcreate()]] - create a new text file, returning a file handle
  
===Closing Text Files in Recital===
+
<pre>
 +
numeric = fcreate(<filename as character>)
 +
</pre>
  
* [[FCLOSE()|fclose()]]
+
* [[FOPEN()|fopen()]] - open an existing text file, returning a file handle
  
===Writing to a Text File using Recital===
+
<pre>
 +
numeric = fopen(<filename as character> [, <mode as numeric>])
 +
</pre>
  
* [[FPUTS()|fputs()]]
+
{| class="wikitable"
 +
!Mode||Description
 +
|-
 +
|||If not specified, read-only
 +
|-
 +
|0||Read-only
 +
|-
 +
|1||Write only.  Existing contents are deleted.
 +
|-
 +
|2||Append.  Text may be added to the end of the existing contents.
 +
|-
 +
|}
  
===Reading From a Text File using Recital===
+
====Closing Text Files in Recital====
* [[FGETS()|fgets()]]
+
 
 +
* [[FCLOSE()|fclose()]] - close a text file opened with [[FOPEN()|fopen()]] or [[FCREATE()|fcreate()]]
 +
 
 +
<pre>
 +
logical = fclose(<filehandle as numeric>)
 +
</pre>
 +
 
 +
====Writing to a Text File using Recital====
 +
 
 +
* [[FPUTS()|fputs()]] - write a character string to a text file, returning the number of bytes written
 +
 
 +
<pre>
 +
numeric = fputs(<filehandle as numeric>, <string as character> [, <bytes as numeric> [, <endofline as character>]])
 +
</pre>
 +
 
 +
====Reading From a Text File using Recital====
 +
* [[FGETS()|fgets()]] - read and return a line from a text file
 +
 
 +
<pre>
 +
character = fgets(<filehandle as numeric> [, <bytes as numeric> [, <endofline as character>]])
 +
</pre>
 +
 
 +
* [[FEOF()|feof()]] - evaluate if the record pointer is at the end of file marker
 +
 
 +
<pre>
 +
logical = feof(<filehandle as numeric>)
 +
</pre>
 +
 
 +
'''Example'''
 +
 
 +
<code lang="recital">
 +
open database southwind
 +
use example
 +
fp=fcreate("names.txt")
 +
count=0
 +
do while not eof()
 +
    count = count + fputs(fp,trim(last_name) + ", "+trim(first_name))
 +
    skip
 +
enddo
 +
fclose(fp)
 +
echo str(count,5) + " bytes written.\n"
 +
 
 +
fp = fopen("names.txt")
 +
count = 0
 +
do while not feof(fp)
 +
    if left(fgets(fp),5) = "Smith"
 +
        ++count
 +
    endif
 +
enddo
 +
fclose(fp)
 +
echo str(count,5) + " Smiths found.\n"
 +
close databases
 +
</code>
  
 
===Checking Whether a File Exists in Recital===
 
===Checking Whether a File Exists in Recital===
* [[FILE()|file()]]
+
* [[FILE()|file()]] - check whether a file exists
 +
 
 +
<pre>
 +
logical = file(<filename as character>)
 +
</pre>
  
 
===Moving, Copying and Deleting Files with Recital===
 
===Moving, Copying and Deleting Files with Recital===
* [[RENAME|rename]]
+
* [[RENAME|rename]] - move a file
  
* [[COPY FILE|copy file]]
+
<pre>
 +
rename <sourcefile as character> [to] <targetfile as character>
 +
</pre>
  
* [[ERASE|erase]]
+
* [[COPY FILE|copy file]] - copy a file
 +
 
 +
<pre>
 +
copy file <sourcefile as character> [to] <targetfile as character>
 +
</pre>
 +
 
 +
* [[ERASE|erase]] - delete a file or files
 +
 
 +
<pre>
 +
erase <filename as character [, filename as character [, ...]]>
 +
</pre>
 +
 
 +
* [[DELETE FILE|delete file]] - delete a file
 +
 
 +
<pre>
 +
delete file <filename as character>
 +
</pre>
  
 
===Accessing File Attributes in Recital===
 
===Accessing File Attributes in Recital===
===Summary===
+
* [[FILEINFO()|fileinfo()]] - return a comma-separated string containing information about a file
 +
 
 +
<pre>
 +
character = fileinfo(<filename as character>)
 +
</pre>
 +
 
 +
* [[FSIZE()|fsize()]] - return the size of a file
 +
 
 +
<pre>
 +
numeric = fsize(<filename as character>)
 +
</pre>
 +
 
 +
* [[FDATE()|fdate()]] - return the last modification date of a file
 +
 
 +
<pre>
 +
date = fdate(<filename as character>)
 +
</pre>
 +
 
 +
* [[FTIME()|ftime()]] - return the last modification time of a file
 +
 
 +
<pre>
 +
character = ftime(<filename as character>)
 +
</pre>
 +
 
 +
* [[FULLPATH()|fullpath()]] - return the full path of a file
 +
 
 +
<pre>
 +
character = fullpath(<filename as character>)
 +
</pre>
 +
 
 +
* [[BASENAME()|basename()]] - return the base filename of a file
 +
 
 +
<pre>
 +
character = basename(<filename as character>)
 +
</pre>
 +
 
 +
===Miscellaneous File and File System Commands and Functions===
 +
* [[DISKSPACE()|diskspace()]] - return the available space on the current disk
 +
 
 +
<pre>
 +
numeric = diskspace()
 +
</pre>

Latest revision as of 13:27, 25 January 2010

Working with Files and File Systems in Recital

Handling Text Files in Recital

Recital includes functions allowing text files to be created, opened and closed and their contents to be read, overwritten or appended to.

Opening and Creating Text Files in Recital

  • fcreate() - create a new text file, returning a file handle
numeric = fcreate(<filename as character>)
  • fopen() - open an existing text file, returning a file handle
numeric = fopen(<filename as character> [, <mode as numeric>])
Mode Description
If not specified, read-only
0 Read-only
1 Write only. Existing contents are deleted.
2 Append. Text may be added to the end of the existing contents.

Closing Text Files in Recital

logical = fclose(<filehandle as numeric>)

Writing to a Text File using Recital

  • fputs() - write a character string to a text file, returning the number of bytes written
numeric = fputs(<filehandle as numeric>, <string as character> [, <bytes as numeric> [, <endofline as character>]])

Reading From a Text File using Recital

  • fgets() - read and return a line from a text file
character = fgets(<filehandle as numeric> [, <bytes as numeric> [, <endofline as character>]])
  • feof() - evaluate if the record pointer is at the end of file marker
logical = feof(<filehandle as numeric>)

Example

open database southwind
use example
fp=fcreate("names.txt")
count=0
do while not eof()
    count = count + fputs(fp,trim(last_name) + ", "+trim(first_name))
    skip
enddo
fclose(fp)
echo str(count,5) + " bytes written.\n"
 
fp = fopen("names.txt")
count = 0
do while not feof(fp)
    if left(fgets(fp),5) = "Smith"
        ++count
    endif
enddo
fclose(fp)
echo str(count,5) + " Smiths found.\n"
close databases

Checking Whether a File Exists in Recital

  • file() - check whether a file exists
logical = file(<filename as character>)

Moving, Copying and Deleting Files with Recital

rename <sourcefile as character> [to] <targetfile as character>
copy file <sourcefile as character> [to] <targetfile as character>
  • erase - delete a file or files
erase <filename as character [, filename as character [, ...]]>
delete file <filename as character>

Accessing File Attributes in Recital

  • fileinfo() - return a comma-separated string containing information about a file
character = fileinfo(<filename as character>)
  • fsize() - return the size of a file
numeric = fsize(<filename as character>)
  • fdate() - return the last modification date of a file
date = fdate(<filename as character>)
  • ftime() - return the last modification time of a file
character = ftime(<filename as character>)
character = fullpath(<filename as character>)
character = basename(<filename as character>)

Miscellaneous File and File System Commands and Functions

  • diskspace() - return the available space on the current disk
numeric = diskspace()