System file I/O tasks
   
mcd = $fopen("file_name" [ , type ] );
    
     
      $fopen
     
    
    opens a disk file for writing, and returns a 32-bit unsigned integer multi-channel descriptor pointer to the file. It returns a zero if the file could not be opened for writing. In Verilog-2001 the
    
     type
    
    indicates how the file is to be opened. The “b” distinguishes a binary file from a text file:
   
     
   
$fclose(mcd);
    
     
      $fclose
     
    
    closes a disk file that was opened by $fopen.
   
$readmemb("file_name", memory_name [ , start_address [ , end_address ]] );
$readmemh("file_name", memory_name [ , start_address [ , end_address ]] );
    
     
      $readmemb
     
    
    and
    
     
      $readmemh
     
    
    initialize a memory array with the values from the file. The file must be an ASCII file with values represented in binary ($readmemb) or hexadecimal ($readmemh). The data values must be the same width as the memory array, and be separated by white spaces. The start and end address are hexadecimal numbers, even for $readmemb, preceded by @.
   
c = $fgetc(mcd);
    
     
      $fgetc
     
    
    reads a byte (a character) from the file.
   
ungetc(c, mcd);
    
     
      $ungetc
     
    
    inserts a specified character into a buffer specified by the file.
   
i = $fgets(str, mcd);
    
     
      $fgets
     
    
    reads a line from the file.
   
i = $fscanf(mcd, "text", signal, signal, ...);
i = $sscanf(str, "text", signal, signal, ...);
    
     
      $fscanf
     
    
    and
    
     
      $sscanf
     
    
    reads data and interprets the data according to a format.
   
i = $fread(reg_or_mem, mcd [ , start_address [ , end_address ]] );
    
     
      $fread
     
    
    reads
    
     
      binary
     
    
    
    
    data from the file.
   
i = $ftell(mcd)
    
     
      $ftell
     
    
    returns the offset from the beginning of the file.
   
i = $fseek(mcd, offset, operation);
i = $rewind(mcd);
    
     
      $fseek
     
    
    sets the position of the next input or output operation on the file.
   
    
     
      $rewind
     
    
    is the same as $fseek(0,0).
   
$fflush( [ mcd ] );
    
     
      $fflush
     
    
    writes any buffered output to the file.
   
i = $ferror(mcd, str);
    
     
      $ferror
     
    
    can be used to obtain more information about an error.
   
$swrite(output_reg, signal, signal, ...));
$sformat(output_reg, text", signal, signal, ...);
    
     
      $swrite
     
    
    and
    
     
      $sformat
     
    
    writes a string to a reg variable. $sformat interprets the string as a format string.
   
Example:
initial begin
  File = $fopen("Result.dat");
  if (!File)
    $display("Could not open \"result.dat\"");
  else begin
    $display(File, "Result is: %4b", A);
    $fclose(File);
  end
end
reg [7:0] Memory [15:0];
initial begin
  $readmemb("Stimulus.txt", Memory);
end 
