Help with file read please

P

Pete Fraser

I'm trying to read a list of stimulus files from a directory file.
This is described on page 527 of Ashenden's book.

At the start of file_loop, read(directory, file_name, file_name_length);
doesn't do what I expected. I assumed it would read an individual
string representing a file name, and would stop when it came to a <CR>
or <LF> . Then on the next pass of the loop it would read the
next filename. Instead, it fills file_name with 50 characters
(including <CR> and <LF>) on each pass.

What am I doing wrong?

Thanks




file_reader : process is
type directory_file is file of string;
file directory : directory_file open read_mode is "stimulus-directory";
variable file_name : string(1 to 50);
variable file_name_length : natural;
variable open_status : file_open_status;
variable char : character;
type char_file is file of character; -- one byte each
file stim_file : char_file;
begin
file_loop : while not endfile(directory) loop
read(directory, file_name, file_name_length);
if file_name_length > file_name'length then
report "file name too long: " & file_name & "... - file skipped"
severity warning;
next file_loop;
end if;
file_open (open_status, stim_file,
file_name(1 to file_name_length), read_mode);
if open_status /= open_ok then
report file_open_status'image(open_status) & " while opening file "
& file_name(1 to file_name_length) & " -file skipped"
severity warning;
next file_loop;
end if;
stimulus_loop : while not endfile(stim_file) loop
if (rst = '1') then
sig <= "00000000";
data_valid <= '0';
elsif (clk'event and clk='1') then
data_valid <= valid;
if (valid = '1') then
read(stim_file, char);
end if;
sig <= char2std(char);
end if;
end loop stimulus_loop;
file_close(stim_file);
end loop file_loop;
wait;
end process file_reader;
 
J

Jonathan Bromley

I'm trying to read a list of stimulus files from a directory file.
This is described on page 527 of Ashenden's book.

At the start of file_loop, read(directory, file_name, file_name_length);
doesn't do what I expected. I assumed it would read an individual
string representing a file name, and would stop when it came to a <CR>
or <LF> . Then on the next pass of the loop it would read the
next filename. Instead, it fills file_name with 50 characters
(including <CR> and <LF>) on each pass.

What am I doing wrong?
file_loop : while not endfile(directory) loop
read(directory, file_name, file_name_length);
if file_name_length > file_name'length then

This error won't ever happen. Using this form of read(),
if the string (line of text) in the file is bigger than file_name,
VHDL truncates the text value appropriately.
end if;
file_open (open_status, stim_file,
file_name(1 to file_name_length), read_mode);

This looks right to me. Are you saying that file_name_length is
equal to 50 every time round the loop? Or are you saying that
file_name_length is 2 bigger than you expect, because the CR/LF
pair is included? It's probably a good idea to add some
diagnostic output each time you read a filename from "directory"...

report "Got filename <" & file_name(1 to file_name_length)
& ">, length = " & integer'image(file_name_length)
severity note;

It's probably much easier to use the TEXTIO package, which deals
correctly with this problem and has guaranteed behaviour across
implementations. Open "directory" as a TEXT file, then read from
it using the READLINE procedure:

variable L: line;
...
while not endfile(directory) loop
-- read a line from the file, without CR/LF:
readline(directory, L);
-- open the corresponding file:
file_open(open_status, stim_file, L.all, read_mode);

The variable of type "line" is in fact a pointer (access type) to
a string, and the readline() procedure automatically allocates a
big enough string to hold the line of text that's read from the
file - hence the use of "L.all" when you come to use the
string in question.

If you're paranoid, like me, it's a smart move to free up the memory
that was allocated for this string, as soon as you're done with it:

deallocate(L);

but in practice you probably don't need to bother, because readline()
automatically does this before creating a new string. However,
the program does have a (probably unimportant) memory leak, because
the last call to readline(), on the very last trip round the loop,
creates a string that will never be deallocated unless you do it
explicitly at the end of the loop.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL, Verilog, SystemC, Perl, Tcl/Tk, Verification, Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail:[email protected]
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

READ FROM FILE 2
Help with Loop 0
Help with EXT3 Filesystem work 1
read binary file 3
Need help with file input.. 1
Reading multiple file 1
READ FROM FILE 3
read from a file 2

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top