Recent

Author Topic: Text files save/open problem  (Read 1230 times)

mpknap

  • Full Member
  • ***
  • Posts: 155
Text files save/open problem
« on: May 28, 2020, 08:47:17 am »

What's wrong with this simple code? error shows in line:
 if status = 1 then
           writeln (outf, line);

as : raised exception class'RunEroor(104)

The procedure loads the list of CSV files from a folder. From each one in turn it copies lines of text after the variable Line: = '...' and is to write the rest to the common OUT.CSV file
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3. var
  4.   PascalFiles: TStringList;
  5.   stan, i: integer;
  6.   f, outf: textfile;
  7.   linia: string;
  8. begin
  9.   PascalFiles := TStringList.Create;
  10.   stan := 0;
  11.   try
  12.     assignfile(outf, 'out.csv');
  13.     rewrite(outf);
  14.     FindAllFiles(PascalFiles, '', '*.csv', True); //find e.g. all pascal sourcefiles
  15.     memo1.Lines.Add(PascalFiles[0]);
  16.  
  17.     for i := 1 to PascalFiles.Count-1 do
  18.     begin
  19.       assignfile(f, PascalFiles[i]);
  20.       try
  21.       append(f);
  22.       stan:=0;
  23.       while not EOF(f) do
  24.       begin
  25.        // memo1.Lines.Add(PascalFiles[i]);
  26.         readln(f, linia);
  27.          if stan=0 then
  28.           if linia =  'time_tag,A7_QUAL_FLAG,A7_COUNT_RATE,A7_FLUX,A8_QUAL_FLAG,A8_COUNT_RATE,A8_FLUX,P8_QUAL_FLAG,P8_COUNT_RATE,P8_FLUX,P9_QUAL_FLAG,P9_COUNT_RATE,P9_FLUX,P10_QUAL_FLAG,P10_COUNT_RATE,P10_FLUX,P11_QUAL_FLAG,P11_COUNT_RATE,P11_FLUX '        then  stan := 1;
  29.  
  30.           if stan = 1 then
  31.           writeln(outf, linia);
  32.  
  33.       end;
  34.       finally
  35.        CloseFile(f);
  36.  
  37.       end;
  38.  
  39.     end;
  40.  
  41.     closefile(outf);
  42.  
  43.  
  44.   finally
  45.     PascalFiles.Free;
  46.   end;
  47.  
  48. end;
                 

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Text files save/open problem
« Reply #1 on: May 28, 2020, 09:11:06 am »
Line 21:  append(f) should be reset(f)?

mpknap

  • Full Member
  • ***
  • Posts: 155
Re: Text files save/open problem
« Reply #2 on: May 28, 2020, 09:14:08 am »
with reset i heave error  Runerror(5) on reset...

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Text files save/open problem
« Reply #3 on: May 28, 2020, 10:13:27 am »
https://www.freepascal.org/docs-html/user/userap4.html

The link above said error 104 happened because the file is not opened using Reset. And error 5 means file access denied.

Maybe the file is not exist, locked by other program or you don't have permission to open it.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Text files save/open problem
« Reply #4 on: May 28, 2020, 10:30:28 am »
Hi!

If you open a file for append then the filepointer points behind the last char of the textfile.
The you try to read from this file - but there is nothing to read.
This results in runerror 5.

Solution: create for every file a second stringlist.
Read the last line.
If it is = 'time_tag,A7_QUAL_FLAG,A7_COUNT_RATE,A  ....'
then continue as in your code,.
Free the second stringlist.

Winni

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Text files save/open problem
« Reply #5 on: May 28, 2020, 11:22:58 am »
You could try this (uses string array rather than the second stringlist suggested by winni):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   PascalFiles: TStringList;
  4.   i, j: Integer;
  5.   outf: TextFile;
  6.   fileList: TStringArray = Nil;
  7.   match: Boolean = False;
  8. begin
  9.   PascalFiles := TStringList.Create;
  10.   try
  11.     FindAllFiles(PascalFiles, '', '*.csv', True);
  12.     if PascalFiles.Count > 0 then
  13.       begin
  14.         SetLength(fileList, PascalFiles.Count);
  15.         for i := 0 to PascalFiles.Count-1 do
  16.           fileList[i] := PascalFiles[i];
  17.       end;
  18.     if Length(fileList) > 0 then
  19.       begin
  20.         AssignFile(outf, 'out.txt');
  21.         try
  22.           Rewrite(outf);
  23.           for i := 0 to High(fileList) do
  24.             begin
  25.               PascalFiles.LoadFromFile(fileList[i]);
  26.               for j := 0 to PascalFiles.Count-1 do
  27.                 begin
  28.                   match := SameText(PascalFiles[j], 'time_tag,A7_QUAL_FLAG,A7_COUNT_RATE,A7_FLUX,A8_QUAL_FLAG,A8_COUNT_RATE,A8_FLUX,P8_QUAL_FLAG,P8_COUNT_RATE,P8_FLUX,P9_QUAL_FLAG,P9_COUNT_RATE,P9_FLUX,P10_QUAL_FLAG,P10_COUNT_RATE,P10_FLUX,P11_QUAL_FLAG,P11_COUNT_RATE,P11_FLUX');
  29.                   if match then
  30.                     begin
  31.                       WriteLn(outf, '"',fileList[i],'" match at line ',j,': ',PascalFiles[j]);
  32.                       match := False;
  33.                       Break;
  34.                     end;
  35.                 end;
  36.               if not match then
  37.                 WriteLn(outf, 'no line of "',fileList[i],' matches');
  38.             end;
  39.         finally
  40.           CloseFile(outf);
  41.         end;
  42.       end;
  43.   finally
  44.     PascalFiles.Free;
  45.   end;
  46. end;
« Last Edit: May 28, 2020, 11:28:13 am by howardpc »

mpknap

  • Full Member
  • ***
  • Posts: 155
Re: Text files save/open problem
« Reply #6 on: May 28, 2020, 01:34:54 pm »
My mistake. The OUT file should not be CSV, only TXT etc. Because I created and opened the OUT.CSV file,   and then hi was counted with others.
And the program tried to open the previously opened file ....

 

TinyPortal © 2005-2018