2011-09-01 18 views

Odpowiedz

6

readline i czytać funkcje powinny osiągnąć to, czego szukasz.

Zasadniczo:

  1. otworzyć plik
  2. Używaj readline dostać następną linię z pliku do bufora linii
  3. Korzystanie odczytu do analizowania bufor liniowy do przydatnych danych
  4. (opcjonalnie) przekształć przeanalizowaną wartość w razie potrzeby.

Fragment kodu:

library STD; 
use std.textio.all; 
... 
variable File_Name   : string; 
file my_file    : text; 
variable lineptr   : line; 
variable temp    : integer; 
... 
file_open(my_file, File_Name, read_mode); -- open the file 
readline(my_file, lineptr); -- put the next line of the file into a buffer 
read(lineptr, temp); -- "parse" the line buffer to an integer 
-- temp now contains the integer from the line in the file 
... 
+0

Ten pan jest smaczną odpowiedzią. Dzięki! –

4

Dla potrzeb odniesienia. Możliwe jest również, aby przekonwertować ciąg do liczby całkowitej za pomocą atrybutu 'value:

variable str : string := "1234"; 
variable int : integer; 
... 

int := integer'value(str); 

zależności od potrzeb własnych może być bardziej pożądane niż procedura read() ponieważ nie destrukcyjnie zmieniać ciąg źródłowy. Działa to jednak tylko wtedy, gdy ciąg jest poprawną liczbą całkowitą bez znaków otaczających innych niż biała spacja.

variable ln : line; 
variable int : integer; 
... 

ln := new string'(" 456 "); -- Whitespace will be ignored 
int := integer'value(ln.all); -- Doesn't consume contents of ln 

ln := new string'("789_000 more text"); 
int := integer'value(ln.all); -- This will fail unlike read() 
Powiązane problemy