如何在VHDL中读取文件时消除空格 [英] How to Eliminate whitespaces while Reading a file in VHDL

查看:162
本文介绍了如何在VHDL中读取文件时消除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在输入文件中有以下数据要读取:

I have below data in an input file to be read:

10101100 11010100 10101100 11010100

11111110 10111001 11111110 10111001

我需要读取每个半字节并将它们写入数组。但是由于空格,线的长度变化,这会影响而len> 0 循环。如何在 readline 之后和读取之前消除该行中的空格,以便在< c> c中获得正确的长度code> len 变量?

I need to read each nibble and write them to an array. But due to the whitespaces the length of the line varies, which affects the while len > 0 loop. How do I eliminate whitespaces from the line after readline and before read so that I would get the proper length in the len variable?

我使用以下代码:

while not endfile(f) loop
    readline(f, L);
    len := L'length;
    while len > 0 loop
        read(L, b);
        mem(i) <= b;
        i := i + 1;
        len := len - b'length;
    end loop;
end loop;

声明:

constant filename:  string := "C:\Users\ChowdaryS\Downloads\topo.bin";
file f:       text open read_mode is filename;
variable L:   line;
variable i:   integer:= 0;
variable b:   std_logic_vector(3 downto 0);  
variable len: integer;


推荐答案

我花了一些时间测试尾随空间的边界条件这需要更改你的嵌套循环:

I spent some time testing the boundary conditions for trailing whilepace which necessitated a change to your nested loops:

        while not endfile(f) loop
            readline(f, L);
            -- len := L'length;
            -- while len >= b'length loop
            while L.all'length >= b'length loop
                read(L, b);
                -- len := L'length;
                mem(i) <= to_stdlogicvector(b);
                i := i + 1;
                -- len := len - b'length;
            end loop;
        end loop;

可以消除len变量。读取L后将包含剩余部分并可以直接检查。

The len variable can be eliminated. After a read L will contain the remainder and can be examined directly.

更改是支持通过复制和粘贴输入文件而不显示的尾随空格字符从您的问题到文件的内容。

The change is to support trailing whitespace characters that don't show up by copying and pasting your input file contents from your question to a file.

我想你会在线上看到尾随空格,这是输入文件写法的假象。

I imagine you're seeing trailing spaces on the line, artifacts of how the input file is written.

以上更改是针对1,2或3个尾随空格的防弹。

The above change is bullet proof for 1, 2 or three trailing spaces.

对于四个或更多尾随空格或一个或多个空格的情况,非阻塞空格或跟踪最后一个有效b值的水平制表符需要扫描任何这些空白字符,你可以依靠琐碎的拒绝。

For the case of four or more trailing spaces or a one or more of a space, a non-blocking space or a horizontal tab trailing the last valid b value a fix requires scanning for any of these whitepace characters, you can count on just trivial rejection.

如果你假设任何非空白字符代表ab值的某些部分,您可以简单地将错误的b值归咎于文件的写入方式。

If you assume any non-whitespace character represents some portion of a b value you can simply blame failure of malformed b values on how the file was written.

这样你就可以简单地测试只剩下空格的字符。

That allows you to simply test for characters remaining that are only whitespace.

增加一个功能:

package body io is
    function iswhitespace (inpstr:  in  string) return boolean is
        constant NBSP: character  := character'val(128); 
    begin
        for i in inpstr'range loop
            if inpstr(i) /= ' ' and inpstr(i) /= NBSP and inpstr(i) /= HT then
                exit;
            elsif i = inpstr'RIGHT then
                return TRUE;
            end if;
        end loop;
        return FALSE;
    end function;

(请记住包裹中的声明)

(keeping in mind the declaration in the package)

textio嵌套循环条件如下所示:

The textio nested loop condition look like:

        -- while L.all'length >= b'length loop
        while L.all'length >= b'length and not iswhitespace(L.all) loop

使文件读取免受一行上任何长度的尾随空格的影响。

which makes file read immune to any length of trailing whitespace on a line.

这篇关于如何在VHDL中读取文件时消除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆