home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
PASCAL
/
PAS_0593
/
PASCAL45.FAQ
< prev
next >
Wrap
Text File
|
1993-05-27
|
4KB
|
96 lines
─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 55 of 345
From : Max Maischein 2:249/6.17 20 May 93 13:38
To : All
Subj : Pascal FAQs Part IV / II
────────────────────────────────────────────────────────────────────────────────
PASCAL.FAQ Frequently asked questions about Pascal
The aim of this document is to give answers to frequently asked
questions in the pascal echo. Thumb rules before asking for help are to
look in the manuals and in the online help first. Many problems can be
solved by just looking into either / both of them. Here are some
topics, that come very often in the Pascal Echo.
Part IV
#7: Typed and untyped files (II)
Q3: How do I delete a line from a text file ?
Q4: How can I skip a header in my file ?
Q5: How can I speed up my file I/O ?
---------------------------------------------------------------------
#7: Typed and untyped files (II)
Q3: How do I delete a line from a text file ?
A3: There is only one way to delete a line from a text file. You will
have to read the source file line by line, check if a line has to be
removed and if not, write the line out. Note that TP has a limit of
255 chars per line, because strings can only hold up to 255 chars.
If you need to accept more than 255 chars per line or have unbound
text from a word editor, this won't work.
Var Infile, OutFile : Text;
S : String;
Function RemoveLine( S : String ) : Boolean;
Begin
RemoveLine := Pos( 'MAX', S ) <> 0;
End;
Begin
OpenFiles;
Repeat
ReadLn( Infile, S );
If not RemoveLine( S )
then WriteLn( OutFile, S );
Until EOF( Infile );
CloseFiles;
End.
Q4: How can I skip a header in my typed file.
A4: There _are_ some dirty ways to skip a header that has not the size
of the other records in a typed file, but I recommend that you use
untyped files and BlockRead() / BlockWrite() then, because the ways
to skip some bytes ini a file work around some stuff of TP. If you
are sure that the proposed skip code works, use it at your own risk.
Q5: How can I speed up my file I/O ?
A5: Use buffers. Today, most machines have a cache installed, but this
dosen't mean, that read / write to / from disk are fast. For text
file IO, you should use SetTextBuf, this will automatically enable
a RAM buffer that minimizes disk accesses with no further hassle for
you. For other files, you will have to read a part of the file in a
buffer in RAM and then process it, this will introduce more
possibilities for errors in your program. You should implement
buffers as the last part in your program so you can be sure that the
rest is running correctly, because if you are working with untyped
files, fencepost errors ( off-by-one-errors ) are frequent !
Pseudocode for using buffers with untyped files :
Const cBufSize = 65520;
Var Buffer : Pointer to Array[ 0..65519 ];
BytesRead : Word;
BufSize : Word;
BufSize := Minimum( cBufSize, MaxAvail );
GetMem( Buffer, BufSize );
While BytesRead <> 0 do
Begin
BlockRead( Infile, Buffer^, BufSize, BytesRead );
ProcessBytesInBuffer( Buffer^, BytesRead );
BlockWrite( OutFile, Buffer^, BytesRead );
End;
---------------------------------------------------------------------
-max
This file is copyrighted by Max Maischein, 2:249/6.17.
No part thereof may be printed without my written permission.
---
* Origin: Arrays of Pointers to Arrays of Pointers to ... (2:249/6.17)