Files c3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Pascal Programming Language

programming II

Omar ElSabek & Fayez Ghazzawi


IT Engineering
3th year – UNKNOWN Department
Course Index :

 Sets
 Record
 Files (Text & Binary)
 Pointers
 Linked Lists
 Unit
Two Types Of Files In Pascal:
1- Text Files
2- Binary Files
Concepts
human readable
editable (for instance by notepad++)
ASCII characters
has lines

Concepts in Pascal:
Sequential (SE)
Redirect standard input/output
open before / close after
read OR write
write = override
A text file (sometimes spelled "textfile": an old alternate name is "flatfile") is a
kind of computer file that is structured as a sequence of lines of electronic text. A
text file exists within a computer file system. The end of a text file is often denoted
by placing one or more special characters, known as an end-of-file marker, after
the last line in a text file.

"Text file" refers to a type of container, while plain text refers to a type of content.
Text files can contain plain text, but they are not limited to such.

from www.wikipedia.com
• We have to open the file when we use it , and
close it when we finish of using it .
• We open the file either to read from it or to write
in it , but we can’t open it for both .
• There is no CTRL+Z here !!, because files are
sequential (no random access) .
• Can’t modify it ! (use another file to help) .
• There is a physical name for each file (URL) .
• To define a text file we write :
Var
F : Text;
• We use “Assign” keyword to link file name
with physical name :
Assign(var F:Text; Filename:String);
• File name can be just name if it’s in the same
folder of the program, but if not , we have to
write all the path of the file .
T-file:
Definition and assign:
Create
Open to write & OVERRIDE Rewrite (logicalFileName);

Open to read Reset (logicalFileName);


Write in it Write (ogicalFileName, StrText);
Writeln (ogicalFileName,StrText);
Read from if Read (ogicalFileName, StrText);
Readln (ogicalFileName,StrText);
Close Close (logicalFileName);

Assign, EOF, EOLN


Examples:
program TextFileProgram;
var
F:Text;
S:String[20];
i,x,y:integer; C:Char;
begin
Assign(F,'MyText');
Rewrite(F);
writeln(F,'i':10,'i*i':10);
for i := 1 to 20 do
write(F,'_');
writeln(F);
for i := 1 to 10 do
writeln(F,i:10,i*i:10);
Close(F);
• If we know the structure of the file :
Reset(F);
Readln(F,S); {the first line}
Writeln(S);
Readln(F,S); {the second line}
Writeln(S);
while not Eof(F) do
begin
Readln(F,x,y);
writeln(x:10,y:10);
end;
Close(F);
• If we don’t know the structure of the file :
Reset(F);
while not Eof(F) do
begin
while not Eoln(F) do
begin
Read(F,C); {C : char}
Write(C);
end;
Readln(F);
Writeln;
end;
Close(F);
• If we don’t know the structure of the file (another) :
Reset(F);
while not Eof(F) do
begin
Readln(F,S);
Writeln(S);
End;
Close(F);

You might also like