number fread(string buffer, number size, number count, number fd)
Parameters
buffer
[out] Storage location for data
size
[in] Item size in bytes
count
[in] Maximum number of items to be read
fd
[in] File descriptor
Description:
The fread function reads up to count items of size bytes from the input file and stores them in buffer.
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count.
If size or count is 0, fread returns 0 and the buffer contents are unchanged.
Return value:
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count
Example
include 'ArrayList.con'
import standard.C.io
import standard.C.math
import standard.C.time
define FILENAME "test.txt"
class Main {
function RandomString(len) {
var buffer = "";
var list = new ArrayList();
list[0] = "a";
list[1] = "b";
list[2] = "c";
srand(time());
for (var i = 0; i<len; i++)
buffer += list[rand()%3];
return buffer;
}
function Main() {
var fd;
var bytes;
var buffer;
// Generate the buffer
buffer = RandomString(10);
// Open file in text mode
if (fd = fopen(FILENAME, "w+t")) {
echo "Contents of buffer = " + buffer + "\n";
bytes = fwrite(buffer, 1, 10, fd);
echo "Wrote " + bytes + " items\n";
fclose(fd);
} else
echo "Problem opening the file\n";
// Open file in text mode
if (fd = fopen(FILENAME, "r+t")) {
buffer = "";
bytes = fread(buffer, 1, 10, fd);
echo "Number of items read = " + bytes + "\n";
echo "Contents of buffer = " + buffer + "\n";
fclose(fd);
} else
echo "File could not be opened\n";
}
}
Results
Contents of buffer = bacccaabba
Wrote 10 items
Number of items read = 10
Contents of buffer = bacccaabba