Recent

Author Topic: Error at begin Code Again  (Read 6619 times)

captian jaster

  • Guest
Error at begin Code Again
« on: July 02, 2010, 09:46:08 pm »
Code: Pascal  [Select][+][-]
  1. Function LinesToArray(Var A:TFileLines; const FPath:String):LongInt;
  2. Var
  3.  FileVar:TextFile;
  4.  Counter:Integer;
  5. begin
  6.   Result := CountFileLines(FPath);
  7.   SetLength(A,Result);
  8.   AssignFile(FileVar,FPath);
  9.   Repeat
  10.   For Counter := 0 to Result Do
  11.   begin
  12.     Readln(FileVar,A[Counter-1]);
  13.   end;
  14.   until(EoF(FileVar));
  15.   CloseFile(FileVar);
  16. end;
  17.  

The Debugger Still says its at my begin code..
What Gives?

Bart

  • Hero Member
  • *****
  • Posts: 5137
    • Bart en Mariska's Webstek
Re: Error at begin Code Again
« Reply #1 on: July 03, 2010, 12:11:45 am »
What error?
When (whilst compiling, linking, running)?

Please try to be more informative when deciding the subject of your posts.

Bart


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9354
  • Debugger - SynEdit - and more
    • wiki
Re: Error at begin Code Again
« Reply #2 on: July 03, 2010, 01:09:20 am »
Code: Pascal  [Select][+][-]
  1.   Repeat
  2.   For Counter := 0 to Result Do
  3.   begin
  4.     Readln(FileVar,A[Counter-1]);
  5.   end;
  6.   until(EoF(FileVar));
  7.  

You just love nesting loops too much, don't you?

Think about this:
- Result contains the amount of lines in the file
- "For Counter := 0 to Result Do"
  After you read all lines of a file, naturally you are at the end of the file.
- "until(EoF(FileVar));"
  So why checking for the end of file, if you know that you must be there?

Even worse, if you where not yet at the end of file, and you would repeat the loop, what would happen?
What if  the file had 5 lines, but then another process added one more line, so now it has 6?
The repeat would loop; because only 5 lines where read, so one more to go.
- But it would attempt to read 5 more, ouch.
- And it would overwrite the previously read entries, since Counter starts at 0 again, ouch.


A few other errors in the code:
  For Counter := 0 to Result Do

If "Result = 1", how many times will the loop run? "for Counter = 0 to 1" => 2 times.
but you would only have one line in the file, and only one entry in the array. What happens win the 2nd run of the loop?


"A[Counter-1]"
Counter starts at 0. So the first entry in A will be "A[-1]" which is out of range....


captian jaster

  • Guest
Re: Error at begin Code Again
« Reply #3 on: July 03, 2010, 01:44:44 am »
Thnx Martin. I fixed the code now  :)
Code: Pascal  [Select][+][-]
  1. Function LinesToArray(Var A:TFileLines; const FPath:String):LongInt;
  2. Var
  3.  FileVar:TextFile;
  4.  Counter:Integer;
  5. begin
  6.   Result := CountFileLines(FPath);
  7.   SetLength(A,Result);
  8.   AssignFile(FileVar,FPath);
  9.   Reset(FileVar);
  10.   For Counter := 0 to Result Do
  11.   begin
  12.     Readln(FileVar,A[Counter]);
  13.   end;
  14.   CloseFile(FileVar);
  15. end;
  16.  
It compiles fine but i get a sig seg V error.
I put the ? symbols next to the code and it Stops and shows that the error is at begin

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9354
  • Debugger - SynEdit - and more
    • wiki
Re: Error at begin Code Again
« Reply #4 on: July 03, 2010, 02:34:35 am »
Thnx Martin. I fixed the code now  :)
Code: Pascal  [Select][+][-]
  1.   For Counter := 0 to Result Do
  2.  
so you fixed 2 out of 3 issues that I listed.....

I put the ? symbols next to the code and it Stops and shows that the error is at begin

If your breakpoints have a ? (even while the app is running), then your compile options aren't correct.
A working breakpoint, is a red point, with no question mark.

Do you get blue dots in front of the lines, when you start your app?

cdbc

  • Hero Member
  • *****
  • Posts: 712
    • http://www.cdbc.dk
Re: Error at begin Code Again
« Reply #5 on: July 03, 2010, 10:47:02 am »
Hi.

Here's another take on the problem...:
Code: [Select]
Function LinesToArray(Var A:TFileLines; const FPath:String):LongInt; 
Var 
 FileVar:TextFile; 
 Counter:Integer; 
begin 
  Result := CountFileLines(FPath); 
  SetLength(A,Result); 
  AssignFile(FileVar,FPath);
 
  // the next block should run just fine...
  Counter:= 0;
  while not EoF(FileVar) do begin
    Readln(FileVar,A[Counter]);
    inc(Counter); 
  end; 
  // don't overcomplicate things, keep it simple :-)

  CloseFile(FileVar); 
end;

hth - Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9354
  • Debugger - SynEdit - and more
    • wiki
Re: Error at begin Code Again
« Reply #6 on: July 03, 2010, 01:13:12 pm »
Here's another take on the problem...:
Code: [Select]
  Counter:= 0;
  while not EoF(FileVar) do begin
    Readln(FileVar,A[Counter]);
    inc(Counter); 
  end; 

And it will crash, if the amount of lines increases (another process writing to the file) between the call to CountFileLines and the repeat loop.

Yes you can increase the size of the array while you are in the loop, and it's how I would do it. But let Jasper explore at what I assume is his level...

cdbc

  • Hero Member
  • *****
  • Posts: 712
    • http://www.cdbc.dk
Re: Error at begin Code Again
« Reply #7 on: July 06, 2010, 11:55:50 am »
Quote
Yes you can increase the size of the array while you are in the loop, and it's how I would do it. But let Jasper explore at what I assume is his level...

+1  :D

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6

 

TinyPortal © 2005-2018