Recent

Author Topic: Problems readig archive files  (Read 12407 times)

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Problems readig archive files
« Reply #15 on: January 24, 2018, 04:09:47 pm »
I have another question.
Now I car read an write the Motorfiles, but i f I create a motorfile and, in the process of creating it, I cancel the filecreation(It means I do not give any File name) I get a run time error. This is a logical development since I lack a sort of "Default name" or something similar. How could I improve the code
   
Code: Pascal  [Select][+][-]
  1. procedure TForm1.WriteFile;
  2.     var
  3.       sl: TStringList;
  4.     begin
  5.       if SaveDialog1.Execute then
  6.       begin
  7.         sl:=TStringList.Create;
  8.         try
  9.           sl.Add ...
  10.           ...
  11.           Showmessage('Motor data saved on File');
  12.         finally
  13.           sl.Free;
  14.       end;
  15.     end;

so that this error do not arise?
Thanks again to all

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Problems readig archive files
« Reply #16 on: January 24, 2018, 04:56:57 pm »
If you do not give a filename, OpenDialog1 will not return TRUE, so this error should not occur.

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Problems readig archive files
« Reply #17 on: January 24, 2018, 05:26:15 pm »
In other words the runtime error you get does not arise from the code snippet you showed.

To get meaningful help from the forum:
  • if you are asking about an error state exactly what it is. Which runtime error? What is the error message?
  • give the full compilable code that produces the error. Then someone else can reproduce it and debug it on their computer instead of having to resort to crystal ball methods to guess at what you have not shown or stated about your set-up.
  • often details of your OS/platform may also be relevant - sometimes 32-bit code is not equivalent to 64-bit code etc.

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: Problems readig archive files
« Reply #18 on: January 24, 2018, 05:42:20 pm »
@jcaser1948

Lots of things can be done to improve the code. Here are some general advices:

- Bad input equals bad output (aka garbage in, garbage out)
Don't trust any data you received. Write some codes to check any input before you use it, save it or continue to the next process. Always check if it is empty or not, and it is in the acceptable range. If it is not valid, show a message to the user and abort the following process, you can use Exit command for this.

- Write the code neatly
Format the code to make it readable by using proper blank lines, spaces, indentations. Give proper names for variables, constants, etc. Provide some comments when needed. You can find and fix bugs much easier if the code is nicely written.

- Use Divide and Conquer strategy
When you find hard to solve problem in programming, try to divide it into several sub processes. Start new project for each of the sub process for testing. Only you're sure they works correctly, then merge it back to the main project. Don't do testings on the 'real' project, it can introduce many new bugs.

- Have enough sleep
Concentration is very important for a programmer. I write buggy codes when I am sleepy. :D

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Problems readig archive files
« Reply #19 on: January 24, 2018, 06:32:09 pm »
If you do not give a filename, OpenDialog1 will not return TRUE, so this error should not occur.

Bart
Dear bart
As Old Galillei woud say"E pui si move", also it happens nevertheless
This is the error I get if a do not give a filename and abort the save dialog.
It is in Spanish, but esy to understand, since most technical words in English are of Latin origin, and have therefore the same roots

El proyecto DatenEingabe ha lanzado una excepción de la clase 'External: SIGSEGV'.

 En archivo 'datenfile1.pas' en linea 2035:
sl.SaveToFile( SaveDialog1.Filename );

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: Problems readig archive files
« Reply #20 on: January 24, 2018, 07:13:45 pm »
I noticed something not correct. I compare your code with howardpc's code. You modified it wrong:

This is original howardpc's code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.WriteFile;
  2. var
  3.   sl: TStringList;
  4. begin
  5.   if SaveDialog1.Execute then
  6.   begin
  7.     sl:=TStringList.Create;
  8.     try
  9.       sl.Add(dia);
  10.       sl.Add(mes);

This is yours:
Code: Pascal  [Select][+][-]
  1. PROCEDURE TForm1.WriteFile;
  2. VAR
  3.     sl: TStringList;
  4.    BEGIN
  5.  
  6.     if SaveDialog1.Execute then
  7.           sl:=TStringList.Create;
  8.       Begin
  9.      sl.SaveToFile( SaveDialog1.Filename );
  10.       try
  11.        sl.Add(dia);
  12.       sl.Add(mes);

Can you see the different?

The problem is on the if-block.

-edit-
I also found another bug. Can you find it?
« Last Edit: January 24, 2018, 07:16:47 pm by Handoko »

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Problems readig archive files
« Reply #21 on: January 26, 2018, 07:37:59 am »
I noticed something not correct. I compare your code with howardpc's code. You modified it wrong:

This is original howardpc's code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.WriteFile;
  2. var
  3.   sl: TStringList;
  4. begin
  5.   if SaveDialog1.Execute then
  6.   begin
  7.     sl:=TStringList.Create;
  8.     try
  9.       sl.Add(dia);
  10.       sl.Add(mes);

This is yours:
Code: Pascal  [Select][+][-]
  1. PROCEDURE TForm1.WriteFile;
  2. VAR
  3.     sl: TStringList;
  4.    BEGIN
  5.  
  6.     if SaveDialog1.Execute then
  7.           sl:=TStringList.Create;
  8.       Begin
  9.      sl.SaveToFile( SaveDialog1.Filename );
  10.       try
  11.        sl.Add(dia);
  12.       sl.Add(mes);

Can you see the different?

The problem is on the if-block.

-edit-
I also found another bug. Can you find it?
Thanks, I see the problem and I have corrected it. Also the other error, consisting sin saving the file at the beginning and at the end again
Thanks, you have helped me a lot.
This matter is solved

 

TinyPortal © 2005-2018