Concept Framework 2.0 documentation Contents | Index

standard.C.io.fwrite

Name Version Deprecated
fwrite version 1.0 no

Prototype:
number fwrite(string buffer, number size, number count, number fd)

Parameters:
buffer      [in] Data to be written
size      [in] Item size in bytes
count      [in] Maximum number of items to be written
fd      [in] File descriptor

Description:
The fwrite function writes up to count items, of size length each, from buffer to the output file. If the file is opened in text mode, each carriage return is replaced with a carriage-return - linefeed pair. The replacement has no effect on the return value.
fwrite returns the number of full items actually written, which may be less than count if an error occurs.

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 

Returns:
fwrite returns the number of full items actually written, which may be less than count if an error occurs.

Documented by Simona Plesuvu, generation time: Fri Jan 21 18:06:19 2011 GMT(c)2011 RadGs Software