File operations
Last updated
Was this helpful?
Last updated
Was this helpful?
To store data, files can be created and managed. There are some functions related to files operations.
The create()
function creates a file.
It returns ‘1’ on file creation and ‘0’ if error and it will mark the flag too.
Example of use
Creating and deleting files example:
The del()
function deletes a file in the current directory. It returns ‘1’ on file delete and ‘0’ if error.
Example of use
The openFile() function opens the filepath if available. It is possible to select a bitwise-inclusive OR of flags from the following list:
O_READ
- Open for reading.
O_WRITE
- Open for writing.
O_RDWR
- Open for reading and writing.
O_APPEND
- If set, the file offset shall be set to the end of the file prior to each write.
O_CREAT
- If the file exists, this flag has no effect except as noted under O_EXCL
below. Otherwise, the file shall be created
O_EXCL
- If O_CREAT
and O_EXCL
are set, open()
shall fail if the file exists.
O_SYNC
- Synchronous writes
Returns '1' on success, '0' otherwise
Example of use
The closeFile()
function closes the pointer which pointed to the previously defined file.
Example of use
The isFile()
function finds a file path in the current directory.
If it exists and it is a file ‘1’ will be returned, ‘0’ will be returned if it exists but it is not a file and ‘-1’ will be returned if it does not exist.
Example of use
The cat()
function stores into the buffer the amount of bytes indicated as input. This function needs three input parameters:
filename: string which defines the filename
offset: number of bytes skipped from the beginning of the file to start reading
scope: number of bytes to read from file from the offset indicated
The catln()
function stores into the buffer the amount of lines indicated as input. This function needs three input parameters:
filename: string which defines the filename
offset: number of lines skipped from the beginning of the file to start reading
scope: number of lines to read from file from the offset indicated
The information is returned as a string where each one of the characters are printed one after the next, EOL (‘\n’) will be encoded as EOL, and will be accounted as one byte.
Example of use
There are several ways to write data to a file:
Indicating the position to start writing. Function writeSD()
. It is possible to indicate the amount of bytes to write.
At the end of the file. Function append()
. It is possible to indicate the amount of bytes to write.
At the end of the file including an EOL character. Function appendln()
It returns ‘1’ on success and ‘0’ if error.
Example of use
The numln()
function counts the number of lines in a file.
The number of lines are counted as the number of ‘\n’ that are found in a file.
It returns the amount of lines and negative value if an error occurred. If there are no lines in file selected, zero will be returned.
Example of use
The getFileSize()
function gets the size of the selected file.
It returns its size in Bytes or ‘-1’ if an error occurs.
Example of use
Available Information
A possible value would be: sizeFile=16
, indicating the size file is 16 Bytes.
The indexOf()
function looks into the file for the first occurrence of the pattern after a certain offset. The algorithm will jump over offset bytes before starting to search for the pattern.
It will return the amount of bytes to the pattern from the offset.
The special characters like ‘\n’ (EOL) are accounted as one byte and files are indexed from 0.
Example of use
Example file “FILE.TXT”
contains: ‘hola caracola\nhej hej\n hola la[EOF]’
The following table shows the results from searching different patterns:
Function
Answer
SD.indexOf(“FILE.TXT”, “hola”, 0);
0
SD.indexOf(“FILE.TXT”, “hola”, 1);
23
SD.indexOf(“FILE.TXT”, “hej”, 3)
11
Creating and deleting files example:
• Reading files example:
• Writing and appending data examples:
• Finding patterns example: