📄 pasl1012.html
字号:
<HTML>
<head>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 13</Title></head>
<body background="../tile01.jpg">
<H1><center>Read the Text Files</center></h1><p><br><br><br><br>
Hello, we meet again. I'm hope you're not bored yet. Now, we will discuss
all things related to text files, like :<ol>
<li>Reading text files</li>
<li>Writing text files</li>
<li>Appending text files</li>
<li>Additional commands : flush</li>
<li>Error handling</li></ol><p>
What is text files actually ? It's just like this file, the read-me files,
AUTOEXEC.BAT and CONFIG.SYS. All files with extension .INI are text files.
This time we will discuss how to write or read one. Look at this :</p><pre>
var
F : text;
</pre><p>
F is a variable of text file. The first thing we must do is associate it
with a file name, for example :</p><pre>
assign(F,'README');
</pre><p>Before you can READ it, you must open it :</p><pre>
reset(F);
</pre><p>You can read the file line by line, just like inputting user. Suppose s is
a string variable :</p><pre>
readln(F, s);
</pre><p>Note that the difference between normal <tt>readln</tt> with this kind of
<tt>readln</tt> is the file variable F.</p><p>
To know if user has reached end-of-file, function <tt>EOF</tt> will return true if
the file has reached the end-of-file (EOF) marker :</p><pre>
if EOF(F) then writeln('This is the end of text file');
</pre><p>After using that file, <b>DON'T FORGET TO CLOSE IT</b>. How ?</p><pre>
close(F);
</pre><p>So, to read out all text file is just like this :</p><hr><pre>
uses crt;
var
F : text;
s : string;
begin
clrscr;
write('Input file name to read : '); readln(s);
assign(F,s); { associate it }
reset(F); { open it }
while not EOF(F) do { read it until it's done }
begin
readln(F,s);
writeln(s);
end;
close(F); { close it }
end.
</pre><hr><p>Easy and simple, right ?</p><p>
How to create one ? First thing you make is the same : associate the text
variable with the filename using <tt>assign</tt> keyword. You then create it using
<tt>rewrite</tt> instead of <tt>reset</tt> :</p><pre>
rewrite(F);
</pre><p>Then, use your logic : To write lines into it use <tt>writeln</tt>
instead of <tt>readln</tt> :</p><pre>
writeln(F,s) { s is a string variable }
</pre><p>And after writing all inside, don't forget to close it with the same
<tt>close</tt>.</p>
<p>So, writing a text file is like this :</p><hr><pre>
uses crt;
var
F : text;
s : string;
begin
clrscr;
write('Input file name to create : '); readln(s);
assign(F,s); { associate it }
rewrite(F); { create it }
writeln('Just enter any text and followed by enter.');
writeln('To quit : Input an empty line.');
repeat
readln(s); { write it until done }
if s='' then break;
writeln(F,s);
until true;
close(F); { close it }
end.
</pre><hr><p><b>
Caution :<br> If you do rewrite an existing file, it means that you delete it
and create a new one.</b></P><p>
How can we just add some text without destroying it ? Well, use <tt>append</tt>
instead. Change the <tt>rewrite</tt> in program above into <tt>append</tt>.
Run them and see the difference.</p><p>
Note : Before you run them, create a two identical text file for test. You
could duplicate this file, but : Don't forget to make a backup for
the original.</p><p>
Pascal uses an internal buffer to store text-information. Buffer is a temporary
place, usually an array or a pointer of a memory location. Pointer
will be discussed in lesson 2. Buffer is employed to speed up (to cache)
the process of reading or writing for any file, including text file. Buffer
is limited in amount, but it is sufficient enough for processes in Pascal.
If the buffer is full, Pascal flushed its contents to disk.</p><p>
Now, sometimes you need to make sure that the buffer is not exceeding its
maximum size. But, wait ... ! You said that it is sufficient enough ? Yes,
it is. But flushing buffer manually can save the day. Say, the power line
is suddenly down and the buffer contents haven't been written to disk. It
IS tragic, isn't it ? You don't want your program user say 'AAAAaaaarrgggh,
my data is LOST !', do you ?</p><p>
Flushing buffer manually is done by adding <tt>flush(f);</tt> somewhere in your
program before you close your file. Manual flush like this can only be done
in text files. How about binary files ? Later, in the next chapter.</p><p>
Now, we found that writing or reading files may not be as smooth as we have
thought. Sometimes, in reading file, the file meant is not found, or having
bad sector so that we cannot read it. If we don't employ the error-handling
routine provided in Pascal, Pascal just shout 'Hey ! This is an error' then
quits the program without notification.</p><p>
This type of error-handling is pretty out-dated style since new error-trapping
hadn't been invented in that time. But, I think it is sufficient for
now, but it is a tiring task to do so. But, think about the user. Don't let
the pain come. Here is the error-handling. Add this :</p><pre>
{$I-} --> to make Pascal calm down when error shows up
: --> Do file process
{$I+} --> to make Pascal detect what error is it if any
</pre><p>
The error can be detected by invoking the function <tt>IOResult</tt>. If <tt>IOresult</tt> is
0, it means no error. For example :</p><hr><pre>
uses crt;
var
F : text;
s : string;
begin
clrscr;
write('Input file name to read : '); readln(s);
assign(F,s); { associate it }
{$I-}
reset(F); { open it }
{$I+}
if IOresult<>0 then
begin
writeln('Error encountered in reading file ',s);
halt;
end;
while not EOF(F) do { read it until it's done }
begin
readln(F,s);
writeln(s);
end;
close(F); { close it }
end.
</pre><hr><p>
Yes, that's easy. Invoking <tt>IOresult</tt> causes the error code to reset. You may
want to save its contents first to detect what error is it. Suppose n is an
integer variable. I just modify a part of above program :</p><hr><pre>
:
:
{$I-}
reset(F);
{$I+}
n:=IOResult;
if n<>0 then
begin
writeln('Error encountered in reading file ',s);
case n of
2: writeln('File not found');
3: writeln('Path not found');
4: writeln('Too many open files');
5: writeln('File access denied');
100: writeln('Disk read error');
101: writeln('Disk write error');
150: writeln('Disk is write protected');
152: writeln('Drive is not ready');
154: writeln('CRC error in data');
156: writeln('Disk seek error');
157: writeln('Unknown media type');
162: writeln('Hardware failure');
else writeln('Various error');
end;
halt;
end;
:
:
</pre><hr><p>
You see that you can detect it. In order to make your program free of error
in run time, you must do this to EVERY input/output command, that means to
every <tt>readln, reset, rewrite, writeln, append</tt>, and <tt>flush</tt>.
Only <tt>assign</tt> and <tt>close</tt> do not need this. Phew, that must be a
very tedious task.</p>
<p>Now that you have learnt error handling. Other error message can be seen
in help (run-time error). Even this chapter is now finished,
I still give you an <b>EXTRA</b> !</p>
<p>This extra is about detecting arguments / parameters passed in our program.
Pascal provide two general "variables" that is : paramcount and paramstr.
Paramcount is a word variable telling the number of parameters that is pas-
sed into our program. Paramstr is an array of string containing the content
s of the parameters. Example :</p><pre>
var
n : word;
begin
for n:=1 to paramcount do
begin
writeln(paramstr[n]);
end;
end.
</pre><p>
How can we test that program ? Should we get out of the IDE and run the EXE
file ourself ? No need ! Borland Pascal provides a 'parameter simulation'.
Check out the menu : Run then choose Parameters... You then asked the para-
meters that will be passed into the program. Write any sentence or words in
it then press enter. Run it and see what happens.
Yes, it was just like running the EXE program in DOS prompt and passing the
parameters manually. Easy, right ?</p><p>
OK, that's all for now. Shall we go to the quiz or the next lesson ? Or you
still don't understand ? Mail <a href="../faq.html">me</a> !</p>
<hr><p><B><H3>Where to go ?</H3></B><p>
<A HREF="../news.html">Back to main page</A><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<a href="pasq1012.html">To the quiz</a><br>
<A HREF="pasl1011.html">Back to Chapter 12</A> about making custom units<BR>
<A HREF="pasl1013.html">To Chapter 14</A> about binary files<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<A HREF="../faq.html">Contact me here</A>
<hr><P class="cpy">By : Roby Joehanes, © 1997, 2000</P>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -