Pascal error 'call by var for arg no.1 has to match exactly' - pascal

I learning to make a program that gets data from a txt file and places it in arrays.
the following are its types :
type
ekspedisi = record
nmeksp : string; // Nama Ekspedisi
jlp : string; // Jenis layanan pengiriman
biaya : integer; // Biaya pengiriman per kg
lp : integer; // per hari
end;
ekspedisiku = record
nom : array [1..100] of ekspedisi;
end;
and a simple algorithm
procedure getDaftarEkspedisi(var kirim : ekspedisiku);
var
i,j,k : integer;
eksp : text;
init : string;
garis : array [1..100] of integer;
mark : string;
jeks : integer;
count : integer;
begin
assign(eksp,'ekspedisi.txt');
reset(eksp);
i := 0;
k := 1;
j := 1;
mark := '|';
jeks := 10;
writeln('Loading ekspedisi.. ');
while(not(eof(eksp))) do
begin
readln(eksp,init);
i := i + 1;
for j := 1 to length(init) do
begin
if init[j] = mark then
begin
garis[k] := j;
k := k + 1;
end;
end;
for i := 1 to jeks do
begin
count := ((i-1)*5);
kirim.nom[i].nmeksp := copy(init,garis[1+count] + 2,garis[2+count]-garis[1+count]-2);
kirim.nom[i].jlp := copy(init,garis[2+count] + 2,garis[3+count]-garis[2+count]-2);
val(copy(init,garis[3+count] + 2,garis[4+count]-garis[3+count]-2),kirim.nom[i].biaya);
val(copy(init,garis[4+count] + 2,garis[5+count]-garis[4+count]-2),kirim.nom[i].lp);
end;
close(kirim);
writeln('loading sukses.');
end;
end;
from that code, i get the following error
<166,13>Error: Call by var for arg no.1 has to match exactly : got "ekspedisiku" expected "Text"
curiously, line 166 is only
close(kirim);
any help is appreciated.

You need to pass the file handle to close, so:
close(kirim);
should be:
close(eksp);
It also looks like you're closing the file at the wrong place in your function. It should most likely be after the while loop, so you need to change:
close(kirim);
writeln('loading sukses.');
end;
end;
to:
end;
close(kirim);
writeln('loading sukses.');
end;
Note that this mistake probably happened because your identation is messed up - if you're careful with formatting your code properly then you won't be so likely to make this kind of error.

Related

launching netsh to get the output

I use this code:
proc := TProcess.Create(nil);
strList := TStringList.Create;
proc.Executable := 'netsh.exe';
proc.Parameters.Add('wlan');
proc.Parameters.Add('show');
proc.Parameters.Add('all');
proc.Options := proc.Options + [poWaitOnExit, poUsePipes];
proc.Execute;
strList.LoadFromStream(proc.Output);
proc.Free;
strList.Free;
The problem is netsh stay on screen (black command window) and do not stop.
If I remove poUsePipes, I can see clearly that netsh do the right job, output is correct.
It works with this code. I've understood the first method (in my question) generate to many bytes in the output so i had a deadlock. Mercy to https://wiki.freepascal.org/Executing_External_Programs (french version for me)
const
READ_BYTES = 2048;
var
Form1: TForm1;
proc: TProcess;
strList: TStringList;
ms: TMemoryStream;
n: LongInt;
br: LongInt;
i: longint;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
ms:=TMemoryStream.Create;
br:=0;
proc := TProcess.Create(nil);
strList := TStringList.Create;
proc.Executable := 'netsh.exe';
proc.Parameters.Add('wlan');
proc.Parameters.Add('show');
proc.Parameters.Add('all');
proc.Options := proc.Options + [poUsePipes];
proc.ShowWindow:=TShowWindowOptions.swoHIDE;
proc.Execute;
while proc.Running do
begin
ms.SetSize(br + READ_BYTES);
n:=proc.Output.Read((ms.Memory + br)^, READ_BYTES);
if n > 0
then begin
Inc(br, n);
end
else begin
//no data: wait 100ms
Sleep(100);
end;
end;
//read last part
repeat
ms.SetSize(br + READ_BYTES);
n:=proc.Output.Read((ms.Memory + br)^, READ_BYTES);
if n > 0
then begin
Inc(br, n);
end
until n <= 0;
ms.SetSize(br);
strList.LoadFromStream(ms);
proc.Free;
for i := 0 to strList.Count - 1 do
begin
memo.Append(CP437ToUTF8(strList.Strings[i]));
end;
strList.Free;
end;

Is it possible to open a Text File in Pascal several times in the same program?

I am trying to make a program that allow me to read a text file and then print it in the terminal.
I just put the simplified parts below so that you see how I think it should work.
My problem is that if for example I open the file a.txt then b.txt it works.
But when I want to open a.txt again, the program stops with an error 217. Same if I want to open another file name c.txt for example. I've spent days on this problem but I do not know where it comes from. I looked on the internet and erorr 217 seems to be related to a non-existing file ? but it is not the case for me...
The error seems to occur on the 'assign' function.
To clarify :
'key' is a Char,
'map' is a two dimension dynamic array of a Record Type.
Repeat
readln(key);
name := key +'.txt';
fileLoading(name, map, maxX, maxY);
Until key = 'l';
...
procedure fileLoading (name : String; var map : PPObjet; var maxX,maxY : Integer);
var
fichier : Text;
i, j : Integer;
chaine : String;
begin
if (FileExists(name)) then
begin
assign(fichier, name);
reset(fichier);
read(fichier,maxX);
readln(fichier,maxY);
if (maxX < 1) or (maxX > MAX) or (maxY < 1) or (maxY > MAX) then
begin
writeln('Tailles invalides');
halt();
end;
allocationTab(maxX, maxY, map);
while (not eof(fichier)) do
begin
for j := 1 to maxY do
begin
readln(fichier,chaine);
for i := 1 to maxX do
begin
case chaine[i] of
'0' : begin
map[j][i].solide := false;
map[j][i].nature := 'v';
map[j][i].valeur := chaine[i];
end;
'1' : begin
map[j][i].solide := true;
map[j][i].nature := 'm';
map[j][i].valeur := chaine[i];
end;
'2'..'9' : begin
map[j][i].solide := false;
map[j][i].nature := 's';
map[j][i].valeur := chaine[i];
end;
end;
end;
end;
end;
end
else
begin
writeln('Erreur le fichier n''existe pas');
halt();
end;
close(fichier);
end;
...
This is the first time I ask a question on stack overflow and I'm not really familiar with it, so I hope my problem is clear enough, as well as my english.
Thanks in advance for all the help you may bring.
try setting
filemode:=0;
before your assign/reset

Not overloading operator

Good day, I'm doing some Codeforces exercises in my free time, and I had a problem to test if the user was a boy or a girl, well, my problem isn't that, i have just demonstrated the code.
While compiling my code in my computer ( I'm using version 3.0.4 for i386 ) i get no error, but codeforces gives me this error
program.pas(15,16) Error: Operator is not overloaded: "freq(Char;AnsiString):LongInt;" + "ShortInt"
program.pas(46,4) Fatal: There were 1 errors compiling module, stopping
The error wasn't clear enough to me, as the same script was perfectly compiled with my version.
The platform is using ( version 3.0.2 i386-Win32 ).
program A236;
uses wincrt, sysutils;
var
username : String;
function freq(char: char; username : String): Integer;
var
i: Integer;
begin
freq:= 0;
for i:= 1 to length(username) do
if char = username[i] then
freq:= freq + 1;
//writeln(freq);
end;
function OddUserName(username : String): Boolean;
var
i, counter: Integer;
begin
OddUserName:= false; // even
counter:= 0;
for i:= 1 to length(username) do
if freq(username[i], username) <> 1 then
delete(username, i, 1)
else
counter:= counter + 1;
if counter mod 2 <> 0 then
OddUserName:= true; // odd
//writeln(counter);
//writeln(OddUserName);
end;
begin
readln(username);
if not OddUserName(username) then
writeln('CHAT WITH HER!')
else
writeln('IGNORE HIM!');
//readkey();
end.
The error is supposed to be at this line probably :
function freq(character: char; username : String): Integer;
Thanks for everyone who helps.
Inside of a function, the function's name can be used as a substitute for using an explicit local variable or Result. freq() and OddUserName() are both doing that, but only freq() is using the function name as an operand on the right-hand side of an assignment. freq := freq + 1; should be a legal statement in modern Pascal compilers, see Why i can use function name in pascal as variable name without definition?.
However, it would seem the error message is suggesting that the failing compiler is treating freq in the statement freg + 1 as a function type and not as a local variable. That would explain why it is complaining about not being able to add a ShortInt with a function type.
So, you will have to use an explicit local variable instead, (or the special Result variable, if your compiler provides that), eg:
function freq(charToFind: char; username : String): Integer;
var
i, f: Integer;
begin
f := 0;
for i := 1 to Length(username) do
if charToFind = username[i] then
f := f + 1;
//writeln(f);
freq := f;
end;
function freq(charToFind: char; username : String): Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(username) do
if charToFind = username[i] then
Result := Result + 1;
//writeln(f);
end;

Pascal Segmentation Fault parsing Text File

I am working on a Question/Answer UI application in Pascal / Lazarus. My problem is that upon invoking below code through a button click, the program crashes with a Segmentation Fault error.
// more declarations... (UI Form, Buttons, ...)
type
TQuestion = class(TObject)
title: string;
answers: array of string;
correct: integer;
end;
var
questions: array of TQuestion;
procedure TForm1.BStartClick(Sender: TObject);
var
i: integer;
j: integer;
line: string;
arrayLength: integer;
question: TQuestion;
stringList: TStringList;
begin
stringList := TStringList.create;
stringList.LoadFromFile('questions.txt');
for i := 0 to stringList.Count - 1 do ;
begin
line := stringList[i];
if (length(line) >= 2) then
if (line[2] = ' ') and ((line[1] = '-') or (line[1] = '+')) then
begin
arrayLength := length(question.answers);
SetLength(question.answers, arrayLength + 1);
question.answers[arrayLength] :=
Copy(line, 2, Length(line) - 1);
if zeile[1] = '+' then
question.correct := arrayLength;
end
else
begin
question := TQuestion.Create;
question.title := line;
arrayLength := length(questions);
setLength(questions, arrayLength + 1);
questions[arrayLength] := question;
end;
end;
BStart.Visible := False;
end;
Well, my Pascal knowledge goes to 10 to 15 years ago. However, I can see that you have an extra semicolon at the end of this line:
for i := 0 to stringList.Count - 1 do ;

How to port this file join function from Delphi to Ruby

I got this delphi function to join files:
procedure Join(Main_FileName, Hidden_Filename : string);
var
MainFile : TFileStream;
HiddenFile : TFileStream;
SizeOfFile : Cardinal;
SearchWord : string[10];
begin
MainFile := TFileStream.Create(Main_FileName, fmOpenReadWrite or fmShareDenyWrite);
try
SizeOfFile := MainFile.Size;
HiddenFile := TFileStream.Create(Hidden_Filename, fmOpenRead or fmShareDenyNone);
try
MainFile.Seek(0, soFromEnd);
MainFile.CopyFrom(HiddenFile, 0);
MainFile.Seek(0, soFromEnd);
finally
HiddenFile.Free;
end;
SearchWord := IntToStr(SizeOfFile) + #0;
MainFile.Seek(0, soFromEnd);
MainFile.WriteBuffer(SearchWord, SizeOf(SearchWord));
finally
MainFile.Free;
end;
end;
And need to port this in ruby.
The created file should compatible with this delphi split function:
(Exists in delphi only no ruby port needed)
procedure Split(Main_FileName, NewFromMain_Filename : string);
var
MainFile : TFileStream;
SplitFile : TFileStream;
HelpStr : string[10];
GetSize : integer;
begin
MainFile := TFileStream.create(Main_FileName, fmOpenReadWrite or fmShareDenyWrite);
try
SplitFile := TFileStream.Create(NewFromMain_Filename, fmCreate or fmShareDenyNone);
try
MainFile.Position := MainFile.Size - 11;
MainFile.Read(HelpStr, 10);
GetSize := StrToInt(HelpStr);
MainFile.Position := GetSize;
SplitFile.CopyFrom(MainFile, MainFile.Size-GetSize);
SplitFile.Size := SplitFile.Size - 11;
MainFile.Size := GetSize;
finally
SplitFile.Free;
end;
finally
MainFile.Free;
end;
end;
Source of the delphi functions in from here:
http://www.delphi-treff.de/tipps/dateienverzeichnisse/dateioperationen/dateien-miteinander-verschmelzen/
Here is the Ruby way - short, readable and easy:
size = File.size("mainfile")
File.open("mainfile", "ab") {|mf|
File.open("hiddenfile", "rb") { |hf|
mf.write(hf.read)
mf.write("#{size}\x0".ljust(10))
}
}
didn't check it split works, have no delphi

Resources