Search code examples
vhdlfile-handling

Reading or writing multiple files using vhdl textio


I have successfully written a vhdl code to read and write a file. Basically I read a file, process the data in the file and write the results to another file. Now I want to extend my program to reading multiple testcases. For example the data will be available in different files named 1.txt, 2.txt, and so on till 100.txt. I want to use a loop to read the data from the files one by one and write the output to different files. Can any one help?

type i1 is file of character;                       
file f1 : i1 open READ_MODE is "1.txt";

this is the type of code i have used for reading the file. How to extend it for array of files?


Solution

  • My blog here http://parallelpoints.com/node/66/ shows how to open files from within a function and read the data.

    You can pass filenames as string type to a function and use text files:

    file file_variable : text;
    

    and...

    file_open(file_variable, filename, read_mode);
    

    to open it.

    Beware, if you are reading binary files, different simulators behave in different ways. Modelsim will do what you (probably) expect. I've had GHDL fall over when an EOF character is read. And Xilinx's simulator expects a special header on the file!

    It's most portable to stick to pure text, even if you have to pre-process it.