2

I would like to write an integer (variable num) on a file (write.txt). Here my code but obviously it does not work. Any suggestion?

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.MATH_REAL.ALL;
library std;
use std.textio.all;

entity file_handle is
end file_handle;

architecture Behavioral of file_handle is

begin

process

variable line_var : line;
file text_var : text;
variable num : integer := 40;

begin        
    file_open(text_var,"C:\Users\Tommy\Desktop\write.txt", write_mode);
    write(line_var, num);         -- write num into line_var
    writeline(text_var, line_var);   -- write line_var into the file
    file_close(text_var);
end process;

end Behavioral;

After running the synthesis, If I open write.txt file, I read b00000000000000000000000000101000. It seems an address or something else. I expected to read 40.

Message Passing
  • 119
  • 3
  • 13
  • Your process will loop. Add a `wait;` statement following `file_close(text_var);`. After doing that, commenting out unnecessary context (`use IEEE.MATH_REAL.ALL; library std;`) and changing the file path your program produces a write.txt file containing the string *40*. IEEE Std 1076-2008 5.7 String representation *For a given value of an integer type, the string representation is the sequence of characters of an abstract literal without a point and whose value is the given value.* b000... doesn't match any abstract literal format (15.5 Abstract literals). Which VHDL tool? –  Sep 20 '15 at 17:36
  • This is not a FPGA question, why do you tag it with FPGA? – FarhadA Sep 21 '15 at 11:18
  • I doubt that you ran synthesis. Wasn't it simulation, instead? Indeed, what do you expect from your synthesiser about this writing to a file? Writing to files is a software concept, not hardware. – Renaud Pacalet Sep 21 '15 at 14:34
  • 1
    Well, `b00000000000000000000000000101000` = 2^3 + 2^5 = 8 + 32 = ... Are there overloads for `write` that allow you to specify the numeric base? –  Sep 21 '15 at 21:32

1 Answers1

4

Try write(line_var, integer'image(num)); instead. It will convert the variable num to a decimal string.

Jonathan Drolet
  • 3,318
  • 1
  • 12
  • 23