2
也許這很簡單,我只是缺少一些基本信息,但我似乎無法在任何地方找到答案。從沒有任何「in」參數的標準輸入讀取函數
我正在寫一類功能Get_Word
,這裏是規範的相關章節文件我的教授寫道:
function Get_Word return Ustring;
-- return a space-separated word from standard input
procedure Fill_Word_List(Wl : in out Ustring_Vector);
-- read a text file from standard in and add all
-- space-separated words to the word list wl
我已經寫了Get_Word
功能,並正嘗試與測試一下驗證碼:
with Ada.Text_IO; use Ada.Text_Io;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure ngramtest is
Name : String(1..80);
File : File_Type;
Size : Natural;
function Get_Word return String is
-- I'm using a strings instead of Unbounded_Strings for testing purposes.
Word : String(1..80) := (others => ' ');
Char : Character;
File : File_Type;
Eol : Boolean;
I : Integer := 1;
begin
--this code below, when uncommented reveals whether or not the file is open.
--if Is_Open(File) then
-- Word := (1..80 => 'y');
--else
-- Word := (1..80 => 'n');
--end if;
loop
Look_Ahead(File, Char, Eol);
if Eol then
exit;
elsif Char = ' ' then
exit;
else
Get (File, Char);
Word(I) := Char;
I := I + 1;
end if;
end loop;
return Word(1..Word'Last);
end Get_Word;
begin
Put ("Enter filename: ");
Get_Line (Name, Size);
Open (File, Mode => In_File, Name => Name(1..Size));
Put (Get_Word);
Close(File);
end ngramtest;
它編譯,但在運行時我得到一個異常告訴我,該文件是不公開的,註釋掉部分返回「NNNNNN ......」,這意味着該文件不中打開功能。
我的問題是我如何從標準輸入讀取,如果我不允許在我的函數中使用參數?沒有他們的功能將無法訪問文件。基本上,我怎樣才能「Get_Word」?
對不起,如果這很簡單,但我完全失去了。
由於一噸!不幸的是,現在我有一個問題,字符串「Word」由於某些原因未被功能修改... – Gesc
該函數適用於我。但是,當你應該從標準輸入讀取時,爲什麼你的測試程序'ngramtest'打開一個文件? –
@Simon Wright - 好吧,讓我備份。 「Fill_Word_List」的要點是讀取.txt文件並返回一個填充文件中每個單詞的數組。它使用'Get_Word'函數來實現這一點。我試圖找到一種方法來實現這一點,但我不能'Get_Word'來讀取文件並返回一個字符串。 我知道標準輸入來自沒有重定向的鍵盤,但我的教授的書寫說'fill_Word_List' '「 - 從標準輸入中讀取文本文件並將所有以空格分隔的字符添加到詞語列表wl」' 我誤解了什麼是標準輸入?我不應該打開一個文件? – Gesc