File tree Expand file tree Collapse file tree 1 file changed +87
-0
lines changed
Expand file tree Collapse file tree 1 file changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ program FileIO;
2+
3+ { $mode objfpc}{ $H+}
4+
5+ uses
6+ SysUtils;
7+
8+ const
9+ DefaultFileName = ' output.txt' ;
10+
11+ function WriteToFile (const FileName: string): integer;
12+ var
13+ F: TextFile;
14+ begin
15+ Result := 1 ; // failure by default
16+ try
17+ AssignFile(F, FileName);
18+ Rewrite(F);
19+
20+ try
21+ Writeln(F, ' A line of text' );
22+ Writeln(F, ' Another line of text' );
23+ Result := 0 ; // success
24+ finally
25+ CloseFile(F);
26+ end ;
27+ except
28+ on E: Exception do
29+ begin
30+ Writeln(' Error writing to file "' , FileName, ' ": ' , E.Message);
31+ end ;
32+ end ;
33+ end ;
34+
35+ function ReadFromFile (const FileName: string): integer;
36+ var
37+ F: TextFile;
38+ Line: string;
39+ LinesRead: integer;
40+ begin
41+ Result := 1 ; // failure by default
42+ LinesRead := 0 ;
43+ try
44+ AssignFile(F, FileName);
45+ Reset(F);
46+
47+ try
48+ while not EOF(F) do
49+ begin
50+ ReadLn(F, Line);
51+ Writeln(Line);
52+ Inc(LinesRead);
53+ end ;
54+ if LinesRead = 0 then
55+ Writeln(' (File "' , FileName, ' " is empty)' );
56+ Result := 0 ; // success
57+ finally
58+ CloseFile(F);
59+ end ;
60+ except
61+ on E: Exception do
62+ begin
63+ Writeln(' Error reading from file "' , FileName, ' ": ' , E.Message);
64+ end ;
65+ end ;
66+ end ;
67+
68+ var
69+ FileName: string;
70+ begin
71+ FileName := DefaultFileName;
72+
73+ if WriteToFile(FileName) <> 0 then
74+ begin
75+ ExitCode := 1 ;
76+ Exit;
77+ end ;
78+
79+ if ReadFromFile(FileName) <> 0 then
80+ begin
81+ ExitCode := 1 ;
82+ Exit;
83+ end ;
84+
85+ ExitCode := 0 ;
86+ end .
87+
You can’t perform that action at this time.
0 commit comments