fread()

Reads a number of bytes from a file to a blob variable.

Synopsis

int fread(file f, blob &b[, int numBytes]);

Parameters

Parameters Meaning
f File to be read
b blob variable
numBytes Number of bytes to be read. This argument is optional, if it is not given all bytes until EOF are read.

Return value

In the event of errors, the function returns -1 or EOF otherwise, the number of read characters.

Description

Reads a number of bytes from a file to a blob variable.

If you set numBytes == 0 (or e.g. reading an empty file) the result will be an empty blob.

Example

Writes 4 bytes and reads 2 bytes to/from a file.

 main()
{
  blob target, rtarget;
  int len, pos;
  string s;
  anytype value;
  bool bigendian;
  s = "FFAB003AFF";
  target = s;
  DebugN(target);
  len = 1;
  value = 5;
  bigendian = TRUE;
  DebugN("Blob AppendValue: " + blobAppendValue(target, value, len, bigendian)); //blob length is 6 = FF AB 0 3A FF 5
  DebugN(target); //ff ab 0 3a ff 5
  int fr, fw, numW, fcl, err, numR;
  file wf;
  wf = fopen("D:\\BFile.txt", "w");
  numR = 2;
  numW = 4;
  err = ferror(wf);
  fw = fwrite(wf, target, numW);
  DebugN("Number of bytes that could be written:", fw);
  //Output: ["Number of bytes that could be written:"][4]
  fr = fread(wf, rtarget, numR);
  DebugN("Number of bytes that could be read:", fr);
  //Output: ["Number of bytes that could be read:"][2]
  fcl = fclose(wf);
}

Assignment

File functions

Availability

CTRL