2014-01-21 106 views
0

好吧,下面的程序打開一個文件,並將第一行中存在的前兩個字符串讀入兩個變量。問題是我一直在試圖看看下面的程序有什麼問題,但是我不明白爲什麼它不起作用。 編譯器編譯它沒有任何問題,但是當我運行它時,它說退出代碼爲2的退出。 問題是該文件存在。 什麼似乎是問題? 任何人都可以幫我嗎? 代碼如下。爲什麼程序退出?

Program num; 

Var 
    f: Text; 
    b, g: String; 
    c: String[1]; 

Procedure thenum (a:String); 
Begin 
    Assign(f,'textfileoffile.txt'); 
    Reset(f); 
    a := 'A'; 
    while not eof(f) and (a <> ' ') do 
    begin 
    Read (f,a); 
    End; 
    Writeln(a); 
End; 

Procedure sth (j:String); 
begin 
    Assign(f,'textfileoffile.txt'); 
    Reset(f); 
    j:='A'; 
    while not seekEoln and eof(f) do 
    begin 
    read(f,j); 
    end; 
    Writeln(j); 
End; 

begin 
    Assign(f,'textfileoffile.txt'); 
    repeat 
    Reset(f); 
    until eof(f); 
    thenum(b); 
    read(f, c); 
    sth (g); 
    if eof(f) then 
    Close(f); 
    Readln; 
End. 
+1

重複重置(f)的重點是什麼?直到eof(f);'loop? –

回答

1

我覺得現在的問題是,您打開相同的文件有很多次

repeat 
Reset(f); 
until eof(f); 

,你打開你的程序文件,我想你必須使用f作爲參數的功能,不要再打開它,或使其全球化

+0

這看起來更好嗎?我試圖在主程序中打開一次文件,但它不起作用。 程序號; Procedure thenum(a:String); 開始 指定(f,'textfileoffile.txt'); 重置(f); a:='A'; 而不是eof(f)和(a「>」')做 開始 閱讀(f,a); 結束; Writeln(a); 結束; 程序sth(j:String); 開始 指定(f,'textfileoffile.txt'); 重置(f); j:='A'; (f,j); 而不尋求埃爾和eof(f)做 開始 讀(f,j); 結束; Writeln(j); 結束; begin 指定(f,'textfileoffile.txt'); 重複 重置(f);直到eof(f); (b); (f,c); (g); (f) 如果eof(f)則 關閉(f); Readln; 結束。 –

+0

你根本不會改變 –

相關問題