Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

File IO Operations: Detecting EOF

Verilog Examples

Runnable example from the Verilog File IO Operations tutorial (Detecting EOF)

Open in IDE
testbench.v
module tb;
  reg [8*45:1] str;
  integer      fd;
  integer      code;

  initial begin
    fd = $fopen("my_file.txt", "r");

    // Keep reading lines until EOF is found
    while (! $feof(fd)) begin

      // Get current line into the variable 'str'
      code = $fgets(str, fd);

      // Display contents of the variable, unless nothing was read
      if (code != 0)
        $write("%0s", str);
    end
    $fclose(fd);
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.