Recent

Author Topic: opening a specific file type and transfer the content into stringgrid  (Read 761 times)

cekirdek

  • Newbie
  • Posts: 4
Hi everyone,

In my homework, i need to open a pks or plt file. ( 2 different open buttons for each)
my program should open these files and write the data inside the file into stringgrid.
then i need to use these values in stringgrid to calculate my final value.

In my stringgrid columns are in this order:

numberofphases, kalpha, hklangle, h, k, l

(these values should be substracted from the pks file)

and i have two additional columns which are:  d and newtheta. These values will be calculated later on by the data on the pks file.

i wrote a code to open this file (shown below) but i received error. Could you please help me?

( i know i am not good on it, but i am trying to learn. thank you for your support ::))

procedure TForm1.OpenClick(Sender: TObject);   
               
var
  i : integer;
  riet : string;


begin
  if OpenDialog1.Execute then

   begin
  AssignFile (myFile, Opendialog1.FileName) ;
  try
    Reset (myFile);
    ReadLn(myFile, riet);

   if   C_FNAME = 'pks' then
    for i :=1  to maxcountpeak do
    begin
      ReadLn (myFile, savednumberofphases, savedkalpha, savedhklangle, savedh, savedk, savedl, mytypearray.hint);
      if EOF (myFile) then
      break;

    end
    else
    Showmessage ('file is not compatible, please try again');

  finally
    CloseFile(myFile);
    WriteValuestoGrid;

  end;

end;

end;                 


« Last Edit: August 07, 2019, 03:23:11 pm by cekirdek »

howardpc

  • Hero Member
  • *****
  • Posts: 3481
Re: opening a specific file type and transfer the content into stringgrid
« Reply #1 on: August 07, 2019, 07:29:51 pm »
What error message do you get?
At which line in the code you show does the error occur if you run in the debugger?

For future reference: requests for help with debugging will tend to attract useful responses only if you show compilable code rather than uncompilable code snippets as here. Then others can reproduce your problem, and perhaps immediately tell you how to rectify it.
I realise your code may not compile, and that is part of the problem. Nevertheless, the full source code (as you have developed it so far) is usually needed for complete strangers on this forum to get an idea of what you are aiming to achieve.

wp

  • Hero Member
  • *****
  • Posts: 7330
Re: opening a specific file type and transfer the content into stringgrid
« Reply #2 on: August 07, 2019, 07:51:11 pm »
I absolutely agree with Howard.

What are savednumberofphases, savedkalpha, savedhklangle, savedh, savedk, savedl, mytypearray.hint? How are they declared? I assume they are arrays because otherwise each ReadLn would overwrite the values read by the previous step. Maybe you did write the array indexes in your post, but they are hidden by the forum software which interprets "i" in square brackets as the instruction to format the following text as italic. It is better to put all embedded code between [code ] ... [/code ] tags which formats the code nicely: Select the text to be formatted as code and select the syntax (usually "Pascal") from the "Code" combobox above the edit field of the forum message editor. This highlights the posted code as Pascal, and retains the array indexes.

But of course, real compilable code is much more helpful. You can add code by packing the *.pas, *.lfm, *.lpi, *.lpr and needed data files of your project into a shared zip file which you can upload under "Attachments and other options". Do not include compiler-generated files (exe, o, ppu, etc) because otherwise the zip file will become too large, and we don't need them anyway because we want to compile your soures ourselves. Another important wish: Do not post your entire project. We don't understand it and usually do not even know how to use it. Instead, write a small demo project which does nothing else than showing the issue that you want to solve.

When these variables indeed are arrays, are these arrays dimensioned correctly? Your code runs from 1 to MaxcountPeak. Where do you get the MaxCountPeak value? When it is a variable quantity you must call "SetLength" for the array to dimension them. But note here that such dynamic arrays begin with index 0, not 1 as you assumed, and when there are n values the last index is n-1 -- this is a "popular" error.

Another logical problem in your code is: What happens with the remaining array values when the end-of-file is reached before your for loop is complete? Or what happens with the rest of the file values when the file contains more data than you prepared by using MaxCountPeak?


Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

cekirdek

  • Newbie
  • Posts: 4
Re: opening a specific file type and transfer the content into stringgrid
« Reply #3 on: August 09, 2019, 11:46:12 am »
Hello howardpc and wp, thank you for your messages.
sorry for my mistake, but i really dont know anything but trying to do something..

Even your answers are difficult to me to understand.
you wrote about arrays, yes it is another problem, i want to solve it after solving this file opening issue.

now, my question is, how can i open a pks file?

 "savednumberofphases, savedkalpha, savedhklangle, savedh, savedk, savedl" are the data i want to read from the pks file:
i want my program to open the pks file and write the data in my stringgrid.

then, i will use all these data and calculate 2 other values, which are "d" and "theta" and they will be also in the stringgrid. for this reason i need arrays.

but first i need to be able to read pks file.

the error i received:

project removed the 'EGridException'. index out of range Cell [Col=8 Row=1]

in file 'grids.pas' at line 9617



maxcount changes from file to file. in pks files, i have 209 rows so i set maxcountpeak to 209. but of course it can be wrong as well...



wp

  • Hero Member
  • *****
  • Posts: 7330
Re: opening a specific file type and transfer the content into stringgrid
« Reply #4 on: August 09, 2019, 12:10:57 pm »
This error message occurs when you write something to a stringgrid cell which does not exist yet.

The first thing that you must do when working with grids is to define the row and column count. When you want to put data from every line of your pks file (which has "maxpeakcount" lines) then you must set:
Code: Pascal  [Select][+][-]
  1.   StringGrid1.RowCount := maxpeakcount+1
The "+1" is for the fixed title row of the grid (requires StringGrid1.FixedRows = 1).
And when you want data in columns "savednumberofpases", "savedkalpha", "savedhklangle", "savedh", "savedk", "savedl", "d", "theta" (8 columns) you must set
Code: Pascal  [Select][+][-]
  1.   StringGrid.ColCount := 8 + 1;
Again, the "+1" is for the fixed column of the grid (StringGrid.FixedCols := 1) into which you can put a line counter.

Only after the row and column count have been set up you can write something to the grid cells.

The problem that I have with your description is that your code in the first post does not contain any access to a string grid at all. So, how can the grid report this error? I think the problem is somewhere else.

Try to learn how to use the debugger.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

wp

  • Hero Member
  • *****
  • Posts: 7330
Re: opening a specific file type and transfer the content into stringgrid
« Reply #5 on: August 09, 2019, 12:30:51 pm »
You wrote in the other thread that you are a chemical engineer, but in the first thread you say this is "homework". I would like to help you, but of course I don't want to write your home work for school, university or whatever. Please explain.

If this is not for homework for school/university I ask you to post a sample pks file, and I can write you a short program how to read this file and store it in a stringgrid. Just to get you started. Of course, i cannot write all follow-up programs for you, in the end you must learn it yourself.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

wp

  • Hero Member
  • *****
  • Posts: 7330
Re: opening a specific file type and transfer the content into stringgrid
« Reply #6 on: August 09, 2019, 03:42:28 pm »
it is probably more instructive to not use a pks file for demonstration, but a file which I created randomly. It has three columns named A, B, and C; and for each column there is some arbitrary numerical value. The first line contains the column titles, the follow lines contain the numbers

In the attached demo I show one possible way how to read the file (there are numerous others) and to store the values in a string grid. The code is written such that the count of files lines is automatically detected.

I added lots of comments to explain what I am doing. Try to understand everything, ask if there are questions. Then apply this knowledge to your pks file (I hope that they have a similar structure).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 3240
Re: opening a specific file type and transfer the content into stringgrid
« Reply #7 on: August 09, 2019, 04:22:29 pm »
Well he could be at a Uni taking chemical Engineering courses  :)

Also, this could be Home too, as in doing work at Home for what ever he does.

I bring home work very often but I get compensated for it .
The only true wisdom is knowing you know nothing

cekirdek

  • Newbie
  • Posts: 4
Re: opening a specific file type and transfer the content into stringgrid
« Reply #8 on: August 09, 2019, 04:46:38 pm »
Dear Wp,

Thank you very much for your help and all efforts.  :)

but still, i am struggling.

my questions:
 
1. you defined the file type as textfile, but my file is not a text file. how can i define my file type? pks is not identified by the program.
2. now program runs but when i click the open button i receive error " in the project exception class 'run error(2) occured. in file unit1.pas at line 236: Resef(F);"

3.in pks file, there is no row which includes the titles of the each column (as you made A,B andC). it directly starts with numbers. attached, i send an example pks (&also a plt) file.

it is probably more instructive to not use a pks file for demonstration, but a file which I created randomly. It has three columns named A, B, and C; and for each column there is some arbitrary numerical value. The first line contains the column titles, the follow lines contain the numbers

In the attached demo I show one possible way how to read the file (there are numerous others) and to store the values in a string grid. The code is written such that the count of files lines is automatically detected.

I added lots of comments to explain what I am doing. Try to understand everything, ask if there are questions. Then apply this knowledge to your pks file (I hope that they have a similar structure).
« Last Edit: August 09, 2019, 04:48:15 pm by cekirdek »

wp

  • Hero Member
  • *****
  • Posts: 7330
Re: opening a specific file type and transfer the content into stringgrid
« Reply #9 on: August 09, 2019, 05:11:03 pm »
(1) No, it is a text file. "Text" file is in contrast to "binary" file. When you open a text file in an editor you see something which you can read. In case of a binary file you would see random characters without a "meaning". To see the difference, just load an exe or zip into an editor.

(2) Runtime error 2 means: "File not found" - see https://www.freepascal.org/docs-html/user/userap4.html. Therefore, I guess there is something wrong with the file name that you use in the program. In my example the file name is given as an argument passed to the procedure "ReadFile" in TForm1.Button1Click.

(3) No title row in pks? Did you read the comments in my demo? There is a line with a simple "ReadLn(F)" which just reads a line and disposes the string; this was done to jump over the title row. So, when there is no title row - just remove this line.

The sample file that you provide shows that there are 6 columns intead of  3 in my example. What do you have to change in my example to catch the new columns as well? (Of course I could have written the program such that it automatically detects how many columns are present, but this would have been a bit more complicated.)

Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018