在 VHDL 中读取文件时如何消除空格
How to Eliminate whitespaces while Reading a file in VHDL
我要读取的输入文件中有以下数据:
10101100 11010100 10101100 11010100
11111110 10111001 11111110 10111001
我需要读取每个半字节并将它们写入数组。但是由于空格,行的长度会发生变化,这会影响 while len > 0
循环。如何消除 readline
之后和 read
之前的行中的空格,以便在 len
变量中获得正确的长度?
我正在使用以下代码:
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;
我花了一些时间测试尾随 whilepace 的边界条件,这需要更改您的嵌套循环:
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会包含余数,可以直接查看。
更改是为了支持尾随的白色space 字符,这些字符不会通过将问题中的输入文件内容复制并粘贴到文件中来显示。
我想你看到了行尾的 spaces,输入文件的写入方式的人工产物。
以上更改是 1、2 或 3 个尾随 space 的防弹措施。
对于四个或更多尾随 space 或一个或多个 space 的情况,非阻塞 space 或水平制表符尾随最后一个有效 b价值修复需要扫描任何这些空白字符,你可以指望只是微不足道的拒绝。
如果您假设任何非白色space 字符代表 b 值的一部分,您可以简单地将格式错误的 b 值的失败归咎于文件的写入方式。
这让您可以简单地测试剩下的只有白色的字符space。
新增功能:
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;
(注意包中的声明)
textio 嵌套循环条件如下:
-- while L.all'length >= b'length loop
while L.all'length >= b'length and not iswhitespace(L.all) loop
这使得文件读取不受一行中任何长度的尾随白色space的影响。
我要读取的输入文件中有以下数据:
10101100 11010100 10101100 11010100
11111110 10111001 11111110 10111001
我需要读取每个半字节并将它们写入数组。但是由于空格,行的长度会发生变化,这会影响 while len > 0
循环。如何消除 readline
之后和 read
之前的行中的空格,以便在 len
变量中获得正确的长度?
我正在使用以下代码:
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;
我花了一些时间测试尾随 whilepace 的边界条件,这需要更改您的嵌套循环:
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会包含余数,可以直接查看。
更改是为了支持尾随的白色space 字符,这些字符不会通过将问题中的输入文件内容复制并粘贴到文件中来显示。
我想你看到了行尾的 spaces,输入文件的写入方式的人工产物。
以上更改是 1、2 或 3 个尾随 space 的防弹措施。
对于四个或更多尾随 space 或一个或多个 space 的情况,非阻塞 space 或水平制表符尾随最后一个有效 b价值修复需要扫描任何这些空白字符,你可以指望只是微不足道的拒绝。
如果您假设任何非白色space 字符代表 b 值的一部分,您可以简单地将格式错误的 b 值的失败归咎于文件的写入方式。
这让您可以简单地测试剩下的只有白色的字符space。
新增功能:
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;
(注意包中的声明)
textio 嵌套循环条件如下:
-- while L.all'length >= b'length loop
while L.all'length >= b'length and not iswhitespace(L.all) loop
这使得文件读取不受一行中任何长度的尾随白色space的影响。