Question on importing text from files

Joined
Jun 10, 2007
Messages
1
Reaction score
0
Hi I've been working on how to make my program manage to input 32 8-bit vectors in from one file (infile.txt) and write changed values into another file (outfile.txt). Here is the code that is relevant to file reading/writing:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
library STD;
use ieee.std_logic_textio.all;
use std.textio.all;

entity quickSorter is
generic (infile : string := "infile.txt";
outfile : string := "outfile.txt");
end quickSorter;

architecture Behavioral of quickSorter is
begin
controller : process
variable tempData : std_logic_vector(7 downto 0);
variable tempString : bit_vector(7 downto 0);
variable sortArray : dataArray;
variable fresult : file_open_status := status_error;
variable tempLine : line;
file in_fd : text open read_mode is infile;
file out_fd : text open write_mode is outfile;
begin

--debug file processes
file_open (fresult, in_fd, infile, read_mode);
if (fresult /= OPEN_OK) then
assert false
report "usage: quickSort"
severity failure;
end if;
file_open (fresult, out_fd, outfile, write_mode);
if (fresult /= open_ok) then
assert false
report "usage: quickSort"
severity failure;
end if;

--read file
while (not endfile(in_fd)) loop
readline (in_fd, tempLine);
read (tempLine, tempString);
end loop;

--misc code
--code which generates conditions for
--the for loop underneath

--write file
for loop
write (tempLine, tempString);
writeLine (out_fd, tempLine);
end loop;

wait;
end process;
end Behavioral;

The entire program compiles correctly, syntax-wise.
However, the simulation always ends on the line after
file_open (fresult, in_fd, infile, read_mode);
which unfortunately means the file is incapable of being opened.
I have added two user document source files to the project (infile.txt and outfile.txt).
I have removed all code besides the relevant infile/outfile coding.
Could someone please help me fix the error?
Thanks for the assistance in advance.
In case of reference, this is run with Xilinx software in vhdl code.
 
Joined
May 4, 2007
Messages
49
Reaction score
0
The file_open command is not needed. Check out the subtle changes I made to your code to see how this works now. You have to place all the incoming data into an array (sortArray) and then write it back out. Generate a counter (v_count) that will tell you how many you read in and how many are needed to write out.

Code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
library STD;
use ieee.std_logic_textio.all;
use std.textio.all;

entity quickSorter is
generic (infile : string := "infile.txt";
outfile : string := "outfile.txt");
end quickSorter;

architecture Behavioral of quickSorter is
begin
controller : process
variable tempData : std_logic_vector(7 downto 0);
variable tempString : std_logic_vector(7 downto 0);
type dataArray is array(1 to 8) of std_logic_vector(7 downto 0);
variable sortArray : dataArray;
variable fresult : file_open_status := status_error;
variable tempLine : line;
variable v_count : integer := 0;
file in_fd : text open read_mode is infile;
file out_fd : text open write_mode is outfile;
begin


--read file
while (not endfile(in_fd)) loop
	v_count := v_count + 1;
readline (in_fd, tempLine);
read (tempLine, tempString);
  sortArray(v_count) := tempString; 
end loop;

--misc code
--code which generates conditions for 
--the for loop underneath

--write file
for i in 1 to v_count loop
  write (tempLine, sortArray(i));
  writeLine (out_fd, tempLine);
end loop;

wait; 
end process;
end Behavioral;
 

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

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top