I have a file abc/xyz.log in log directory. How can I find if the file exists or not in SystemVerilog class. If the file exists I want to delete the file.
Thanks.
I have a file abc/xyz.log in log directory. How can I find if the file exists or not in SystemVerilog class. If the file exists I want to delete the file.
Thanks.
Using the Unix utility "test" (see "man test") through a $system call can do the job. It can also be done through direct C using the stat() function (much more cumbersome). This example can be leveraged to do what you want. In this case it checks for the existence of a directory and it not found, attempts to create it.
string report_dir;
report_dir = {SIM_ROOT,"/",PROJ_NAME,"/",TECH_PROCESS,"/",LOGNAME,"/doc/functional"};
if ($system($sformatf("/usr/bin/test -d %s", report_dir)) != 0) begin
$display("INFO: creating output directory %s", report_dir);
if($system($sformatf("mkdir -p %s", report_dir)) != 0) begin
$display("ERROR: mkdir -p %s returned an error", report_dir);
$fatal(0);
end
end
if($system($sformatf("/usr/bin/test -d %s -o -w %s", report_dir, report_dir)) != 0) begin
$display("ERROR: output directory %s does not exist or is not writeable", report_dir);
$fatal(0);
end
You can use
$fopen
and it returns 0 if it doesn't exist. If it does exist,$fclose
the file and then use$system("shell command")
to delete it.