问题
I am trying to understand how reading a file works in vhdl if I open a file, read through it, test for end of file, close the file and then re open that file and then start to read again will it start from the beginning of the file?
many thanks
回答1:
Look to the authority on the VHDL language for your answer.
IEEE Std 1076-2008
5.5 File types
5.5.1 GeneralA file type definition defines a file type. File types are used to define objects representing files in the host system environment. The value of a file object is the sequence of values contained in the host system file.
5.5.2 File operations
The FILE_OPEN procedures open an external file specified by the External_Name parameter and associate it with the file object F. If the call to FILE_OPEN is successful (see the following), the file object is said to be open and the file object has an access mode dependent on the value supplied to the Open_Kind parameter (see 16.3).
— If the value supplied to the Open_Kind parameter is READ_MODE, the access mode of the file object is read-only. In addition, the file object is initialized so that a subsequent READ will return the first value in the external file. Values are read from the file object in the order that they appear in the external file.
...
If a file object F is associated with an external file, procedure FILE_CLOSE terminates access to the external file associated with F and closes the external file. If F is not associated with an external file, then FILE_CLOSE has no effect. In either case, the file object is no longer open after a call to FILE_CLOSE that associates the file object with the formal parameter F.
...
Procedure READ retrieves the next value from a file; it is an error if the access mode of the file object is write-only or if the file object is not open. Procedure WRITE appends a value to a file. Procedure FLUSH requests that the implementation complete the effect of all previous calls to the WRITE procedure for a file. For the WRITE and FLUSH procedures, it is an error if the access mode of the file object is read-only or if the file is not open. Function ENDFILE returns FALSE if a subsequent READ operation on an open file object whose access mode is read-only can retrieve another value from the file; otherwise, it returns TRUE. Function ENDFILE always returns TRUE for an open file object whose access mode is write-only. It is an error if ENDFILE is called on a file object that is not open.
Further reading will reveal that file access is exclusive, you can't execute another FILE_OPEN without a FILE_CLOSE. You can demonstrate this by finding a subsequent FILE_OPEN without a preceding FILE_CLOSE will return a STATUS_ERROR.
So a FILE_OPEN starts access from the beginning. The only way to reset to the beginning of a file is to FILE_CLOSE then FILE_OPEN.
回答2:
Just for completeness - this depends on whether you're using VHDL-87 (unlikely now), or 93+. There are no seek
/rewind
/etc operations, but the file pointer is reset when you open the file. If you want to seek
, or read through the file multiple times, you need to close/open on each occasion. For VHDL-87, the file is opened when the file declaration is elaborated (during normal elaboration if you declare the file in an architecture or process, or when a procedure is executed when declared in a procedure). So, in '87, you read the file multiple times by calling your procedure multiple times.
'93 introduced file_open
/file_close
. If you want to do lots of file handling, you should probably do it in C/C++ via your simulator's VHPI.
来源:https://stackoverflow.com/questions/32267035/is-there-anyway-to-read-through-a-file-multiple-times-in-vhdl-using-std-textio