1
0

Add text_file example, and partially implemented simple_expressions example

This commit is contained in:
Timothy Warren 2021-09-30 10:22:35 -04:00
parent 36bc2ceed9
commit be71569f56
3 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,18 @@
program simple_expressions;
{$mode objfpc}{$H+}
var
b: Byte = 4;
c: Byte = 3;
begin
WriteLn('Initial value of b is ', b, '; initial value of c is ', c, sLineBreak);
b := b shr 1; // >> shift right
c := shl 4; // << shift left
WriteLn('After b shr 1, b is ', b, '; after c shl 4, c is ', c, sLineBreak);
WriteLn('c xor b = ',c xor b, '; c and b = ', c and b, '; c or b,'
{$IFDEF WINDOWS}
ReadLn;
{$ENDIF}
end.

60
text_file/text_file.lpi Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="text_file"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams>
<Units Count="1">
<Unit0>
<Filename Value="text_file.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="text_file"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

47
text_file/text_file.lpr Normal file
View File

@ -0,0 +1,47 @@
program text_file;
{$mode objfpc}{$H+}
uses sysutils;
var
txtF: TextFile;
s: String;
procedure ReadTenLines;
const lineNo: integer = 0;
var linesRead: integer = 0;
begin
while not EOF(txtF) and (linesRead < 10) do
begin
ReadLn(txtF, s); // Read the current line in the file, and store in s
Inc(linesRead); // linesRead += 1
Inc(lineNo);
s := Format('Line %d: %s', [lineNo, s]);
WriteLn(s);
end;
end;
begin
AssignFile(txtF, 'text_file.lpr'); // Open pointer to the file
Reset(txtF); // Open existing file
WriteLn('Lines from text_file.lpr will be displayed 10 at a time');
WriteLn;
// Read and display the file
try
while not EOF(txtF) do
begin
ReadTenLines;
WriteLn;
WriteLn('Press [Enter] to continue');
ReadLn;
end;
finally
CloseFile(txtF);
end;
Write('End of file reached. Press [Enter] to finish');
ReadLn;
end.