Forum > General

Error at begin Code Again

(1/2) > >>

captian jaster:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function LinesToArray(Var A:TFileLines; const FPath:String):LongInt;Var FileVar:TextFile; Counter:Integer;begin  Result := CountFileLines(FPath);  SetLength(A,Result);  AssignFile(FileVar,FPath);  Repeat  For Counter := 0 to Result Do  begin    Readln(FileVar,A[Counter-1]);  end;  until(EoF(FileVar));  CloseFile(FileVar);end; 
The Debugger Still says its at my begin code..
What Gives?

Bart:
What error?
When (whilst compiling, linking, running)?

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

Bart

Martin_fr:

--- Quote from: captain jaster on July 02, 2010, 09:46:08 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  Repeat  For Counter := 0 to Result Do  begin    Readln(FileVar,A[Counter-1]);  end;  until(EoF(FileVar)); 
--- End quote ---

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:
Thnx Martin. I fixed the code now  :)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function LinesToArray(Var A:TFileLines; const FPath:String):LongInt;Var FileVar:TextFile; Counter:Integer;begin  Result := CountFileLines(FPath);  SetLength(A,Result);  AssignFile(FileVar,FPath);  Reset(FileVar);  For Counter := 0 to Result Do  begin    Readln(FileVar,A[Counter]);  end;  CloseFile(FileVar);end; 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:

--- Quote from: captain jaster on July 03, 2010, 01:44:44 am ---Thnx Martin. I fixed the code now  :)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  For Counter := 0 to Result Do 
--- End quote ---
so you fixed 2 out of 3 issues that I listed.....


--- Quote from: captain jaster on July 03, 2010, 01:44:44 am ---I put the ? symbols next to the code and it Stops and shows that the error is at begin

--- End quote ---

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?

Navigation

[0] Message Index

[#] Next page

Go to full version