請(qǐng)?jiān)谀愕?a target="_blank" rel="nofollow">CP Pascal Editor里面做下面文章中的測試哦。
首先通過一個(gè)例子來展示pascal
讀寫txt
文件的方式:
Program Salaries;
Var F:Text;
Salary,max:integer;
Name:String[20];
MaxName:String;
Sex:0..1;
Begin
Max:=0;
Assign(F,'E:\Exam\Salary.txt');
Reset(F);
While not Eof(F) Do
Begin
ReadLn(F,Name,Sex,Salary);
If (Sex=1) and (Salary>Max) then
Begin
Max:=Salary;
MaxName:=Name;
End
End;
Close(F);
Writeln;
WriteLn('The highest ladis salary is for Mrs. ',MaxName,'By Value:',Max);
Writeln;
Writeln;
Writeln;
Writeln;
WriteLn('Press Enter To Exit');
ReadLn
End.
運(yùn)行代碼之前,在你的E:\Exam\
,放進(jìn)去一個(gè)Salary.txt
文件
.txt
文件的內(nèi)容如下,使用空格或者tab分隔一下:
Peter 0 3500
Helena 1 2000
John 0 0
Natalia 1 7000
Frank 0 7100
運(yùn)行程序,你會(huì)發(fā)現(xiàn):這個(gè)程序計(jì)算出了女性員工中的工資最高者。
解釋: .txt
文件中,1代表女性,0代表男性。
代碼中的eof
的意思是:end of file。
Assign
是分配,賦值的意思。
創(chuàng)建txt文件
下面做一件最簡單的事情,創(chuàng)建一個(gè)txt
文件,往里面寫一些東西。
請(qǐng)運(yùn)行下面的代碼:
Program Lesson9_Program2;
Var FName, Txt : String[200];
UserFile : Text;
Begin
FName := 'PASCAL_Textfile';
Assign(UserFile,'C:\'+FName+'.txt'); {assign a text file}
Rewrite(UserFile); {open the file 'fname' for writing}
Writeln(UserFile,'PASCAL PROGRAMMING');
Writeln(UserFile,'要是你有啥不明白的,');
Writeln(UserFile,'請(qǐng)記得仔細(xì)閱讀我在簡書上寫的文章哦:');
Writeln('Write some text to the file:');
Readln(Txt);
Writeln(UserFile,'');
Writeln(UserFile,'The user entered this text:');
Writeln(UserFile,Txt);
Close(UserFile);
End.
運(yùn)行完畢,請(qǐng)你到自己的C盤下面,找到你剛剛創(chuàng)建的文件:PASCAL_Textfile.txt
看看。
修改代碼,自己繼續(xù)嘗試10分鐘,讓自己熟悉這個(gè)代碼。
重寫txt文件
Var UFile : Text;
Begin
Assign(UFile,'C:\PASCAL_Textfile.TXT');
ReWrite(UFile);
Writeln(UFile,'How many sentences, ' + 'are present in this file?');
Close(UFile);
End.
在原來的txt文件后面,追加(append)內(nèi)容:
Var UFile : Text;
Begin
Assign(UFile,'C:\PASCAL_Textfile.TXT');
Append(UFile);
Writeln(UFile,'append: hahahahaha, '+
'oh my gooooooooooooood');
Close(UFile);
End.
重復(fù)執(zhí)行上述的代碼,你會(huì)發(fā)現(xiàn),C:\PASCAL_Textfile.txt
文件后面會(huì)一次又一次追加上新的內(nèi)容:
append: hahahahaha, oh my gooooooooooooood
刪除文件
Var UFile : Text; { or it could be of 'file' type}
Begin
Assign(UFile,'C:\\PASCAL_Textfile.txt');
Erase (UFile);
End.
此時(shí)你會(huì)發(fā)現(xiàn)文件被刪除了。
一些別的用法,你可以參考這個(gè)網(wǎng)頁.
最后,請(qǐng)完成下面的操作:
在C盤
新建文件file1.txt
往里面隨意寫入一些文字,保存。
運(yùn)行代碼如下:
program CopyOneByteFile;
var
mychar : string[200];
filein, fileout : text;
begin
assign (filein, 'c:\file1.txt');
reset (filein);
assign (fileout, 'c:\file2.txt');
rewrite (fileout);
read (filein, mychar);
write (fileout, mychar);
close(filein);
close(fileout)
end.
查看C盤中file2.txt
的內(nèi)容。
你是不是知道了,上面是一個(gè)操作文件的示例,程序運(yùn)行在DOS下,將讀取file1.txt
內(nèi)容,寫入到file2.txt
中。
這個(gè)過程之中,要是file2.txt
不存在,那么它將被創(chuàng)建。
下面的網(wǎng)站上有不少例子可以參考:
http://pascal-programming.info/lesson9.php
http://wiki.freepascal.org/Object_Pascal_Tutorial/zh_CN
http://www.tutorialspoint.com/pascal/pascal_variable_types.htm
http://wiki.freepascal.org/Lazarus_Documentation/zh_CN
http://progopedia.com/implementation/turbo-pascal/
https://www.daniweb.com/software-development/pascal-and-delphi/threads/50487/pascal-examples