Help with file read please

Discussion in 'VHDL' started by Pete Fraser, Dec 20, 2004.

  1. Pete Fraser

    Pete Fraser Guest

    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;
    Pete Fraser, Dec 20, 2004
    #1
    1. Advertising

  2. On Sun, 19 Dec 2004 16:32:57 -0800, "Pete Fraser" <>
    wrote:

    >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:
    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.
    Jonathan Bromley, Dec 21, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    492
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    524
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,721
    Mike Hewson
    Jan 7, 2005
  4. Sreejith K
    Replies:
    24
    Views:
    990
    Sreejith K
    Mar 24, 2009
  5. Alex Dowad
    Replies:
    4
    Views:
    258
    Michel Demazure
    May 1, 2010
Loading...

Share This Page