File IO functions

Hi ALL,

I was trying to use file IO functions to open file, write to the file at random offset and then read the file from the offset. Following is the code snippet. Note: file descriptor is used from different write and read function within class to write and read file

//open the file
function new()
::::::::::::::
::::::::::::::


file_mcd = $fopen("disk_mem.log","w+");

::::::::::::::
::::::::::::::
endfunction

function write_file(int addr)

::::::::::::::
::::::::::::::

//rewind the file ptr
if($fseek(file_mcd,0,0)== -1)$display("fseek failed...");
//reposition the file ptr
if($fseek(file_mcd,addr,0)== -1)$display("fseek failed...");
data = $urandom();
$fdisplay(file_mcd,"%x",data);

::::::::::::::
::::::::::::::


endfunction


function read_file(int addr)


::::::::::::::
::::::::::::::

//rewind the file ptr
if($fseek(file_mcd,0,0)== -1)$display("fseek failed...");
//reposition the file ptr
if($fseek(file_mcd,addr,0)== -1)$display("fseek failed...");
file_result = $fscanf(file_mcd,"%x",read_data)
if(file_result == 0)$display("fscanf failed...");

::::::::::::::
::::::::::::::


endfunction

The write_file function is called multiple times and then read_file is called to read the data from the file with the same addr it was written to. The problem is even though in the read_file when I call $fseek to repositioned the file ptr it always read the last position written to the file.

Any help will be highly appreciated.

Thanks