Text files save/open problem lazarus / 13-General ---------------------------------------------------------------------- mpknap · 2020-05-28 06:47 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject); var  PascalFiles: TStringList;  stan, i: integer;  f, outf: textfile;  linia: string;begin  PascalFiles := TStringList.Create;  stan := 0;  try    assignfile(outf, 'out.csv');    rewrite(outf);    FindAllFiles(PascalFiles, '', '*.csv', True); //find e.g. all pascal sourcefiles    memo1.Lines.Add(PascalFiles[0]);     for i := 1 to PascalFiles.Count-1 do    begin      assignfile(f, PascalFiles[i]);    try      append(f);      stan:=0;      while not EOF(f) do      begin       // memo1.Lines.Add(PascalFiles[i]);        readln(f, linia);         if stan=0 then          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;           if stan = 1 then          writeln(outf, linia);       end;      finally       CloseFile(f);     end;     end;     closefile(outf);    finally    PascalFiles.Free;  end; end; ---------------------------------------------------------------------- bytebites · 2020-05-28 07:11 Line 21:  append(f) should be reset(f)? ---------------------------------------------------------------------- mpknap · 2020-05-28 07:14 with reset i heave error  Runerror(5) on reset... ---------------------------------------------------------------------- Handoko · 2020-05-28 08:13 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 · 2020-05-28 08:30 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 · 2020-05-28 09:22 You could try this (uses string array rather than the second stringlist suggested by winni):Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  PascalFiles: TStringList;  i, j: Integer;  outf: TextFile;  fileList: TStringArray = Nil;  match: Boolean = False;begin  PascalFiles := TStringList.Create;  try    FindAllFiles(PascalFiles, '', '*.csv', True);    if PascalFiles.Count > 0 then      begin        SetLength(fileList, PascalFiles.Count);      for i := 0 to PascalFiles.Count-1 do          fileList[i] := PascalFiles[i];      end;    if Length(fileList) > 0 then      begin      AssignFile(outf, 'out.txt');        try          Rewrite(outf);        for i := 0 to High(fileList) do            begin              PascalFiles.LoadFromFile(fileList[i]);              for j := 0 to PascalFiles.Count-1 do                begin                  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_FL UX,P11_QUAL_FLAG,P11_COUNT_RATE,P11_FLUX');                  if match then                    begin                      WriteLn(outf, '"',fileList[i],'" match at line ',j,': ',PascalFiles[j]);                    match := False;                      Break;                  end;                end;              if not match then              WriteLn(outf, 'no line of "',fileList[i],' matches');            end;        finally          CloseFile(outf);        end;      end;  finally    PascalFiles.Free;  end;end; ---------------------------------------------------------------------- mpknap · 2020-05-28 11:34 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 ....