Recent

Author Topic: TStringList.SaveToFile error  (Read 10320 times)

William Marshall

  • Jr. Member
  • **
  • Posts: 52
TStringList.SaveToFile error
« on: September 04, 2018, 09:39:01 pm »
     I'm trying to delete a specific line from a text file using a StringList.

Code: Pascal  [Select][+][-]
  1. procedure DeleteSubstanceFromAllFiles;
  2. var FileList, DataList : TStringList;
  3.     SubstanceName : string;
  4.     Node : TTreeNode;
  5.     FileNode : TTreeNode;
  6.     i, j : integer;
  7.     F : text;
  8.     DataRec : string;
  9.  
  10. begin
  11.   { Get the substance to be deleted: }
  12.   Node := frmSubsLister.tvSubstances.Selected;
  13.   SubstanceName := Node.Text;
  14.  
  15.   { Make a list of the files containing SubstanceName: }
  16.   FileList := TStringList.Create;
  17.   FileNode := Node.GetFirstChild;
  18.   for i := 0 to Node.Count - 1 do
  19.     begin
  20.       FileList.Add(FileNameFromNodeText(FileNode.Text));
  21.       FileNode := FileNode.GetNextSibling;
  22.     end;
  23.  
  24.   { Delete SubstanceName from each file: }  
  25.   DataList := TStringList.Create;
  26.   for i := 0 to FileList.Count - 1 do
  27.     begin
  28.  
  29. //AssignFile(F, FullCSPFileName(FileList[i]));
  30. //Reset(F);
  31.  
  32.       DataList.Clear;  //Needed?
  33.       DataList.LoadFromFile(FullCSPFileName(FileList[i]));
  34.       for j := 1 to DataList.Count - 1 do  //Ignore the first line
  35.         begin
  36.           DataRec := DataList[j];
  37.           if (GetName1(DataRec) = SubstanceName) or (GetName2(DataRec) = SubstanceName) or (GetCommonName(DataRec) = SubstanceName) then
  38.             begin
  39.               DataList.Delete(j);
  40.  
  41. //CloseFile(F);
  42. //Rewrite(F);  // -> RunError(5) - "File access denied"
  43. //Erase(F);  // -> RunError(5) - "File access denied"
  44. //RenameFile(FullCSPFileName(FileList[i]),FullCSPFileName(FileList[i])+'.x');  //File not renamed
  45.  
  46.               DataList.SaveToFile(FullCSPFileName(FileList[i]));  // -> "Unable to create file"
  47.               Break;
  48.             end;
  49.         end;  //for j
  50.     end;  //for i
  51.  
  52.   FreeAndNil(FileList);
  53.   FreeAndNil(DataList);

But the program stops at the SaveToFile line (line 46) with the error "Unable to create file".  In desperation I've tried closing the file, rewriting the file ( -> "File access denied"), erasing the file ( -> "File access denied"), and renaming the file ( -> no error, though the file was not renamed).  I'm sure I'm missing something simple, but what?  (I guess I could explain what's going on in the first half of the procedure, but everything seems to work properly -- the correct line in the file is found and deleted from DataList.)
     Thanks for your help.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TStringList.SaveToFile error
« Reply #1 on: September 04, 2018, 10:20:17 pm »
Just a quick test: are you sure you can write to the file? As in, do you have the neccessary permissions?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TStringList.SaveToFile error
« Reply #2 on: September 04, 2018, 10:28:12 pm »
Savetostream and loadfromstream. Make sure you set the correct file access for the TFilestream...
Otherwise it is a rights issue on the OS level.
A TStringlist by itself has not many options, A TFilestream has...
Also
If fileexists then deletefile will maybe help. THEN save the Tstringlist.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: TStringList.SaveToFile error
« Reply #3 on: September 04, 2018, 10:34:47 pm »
Thaddy beat me to it, I was going to suggest to delete the file and write a new one with the same filename.

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: TStringList.SaveToFile error
« Reply #4 on: September 04, 2018, 10:55:08 pm »
     These are all text files I have created and edited using the program.  They are always converted to StringLists with LoadFromFile and saved using SaveToFile in several places in the program.  The file I've been using to test the procedure definitely exists, and I have permission to use it.  As I said, the procedure works up to the SaveToFile line.  I can use a stream, I guess, but isn't that what SaveToFile does anyway?
     I've tried deleting the file (with Erase - maybe the wrong one to use?), but I get the "File access denied" error.
« Last Edit: September 04, 2018, 10:58:05 pm by William Marshall »
Lazarus 1.8.0; fpc 3.0.4; Windows 10

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: TStringList.SaveToFile error
« Reply #5 on: September 04, 2018, 11:23:18 pm »
Sounds like a permission error.

Make sure you're running the program with the correct user.

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: TStringList.SaveToFile error
« Reply #6 on: September 05, 2018, 12:14:38 am »
     I'm sorry, but I don't see how it could be a permissions problem.  I'll admit I don't have a complete understanding of what "permissions" means, but I've created all of these text files, read them, edited them, and saved them many, many times in other parts of the program with no problems.  I just edited and saved this file again a couple minutes ago.  It's just this new part that doesn't behave.  It seems (to me) the problem may be the file name somehow, but LoadFromFile accepts it, and it doesn't change between LoadFromFile and SaveToFile. 
Lazarus 1.8.0; fpc 3.0.4; Windows 10

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: TStringList.SaveToFile error
« Reply #7 on: September 05, 2018, 12:24:45 am »
Did you open the file at some other part of the program, but did not close it?

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: TStringList.SaveToFile error
« Reply #8 on: September 05, 2018, 12:34:59 am »
Nope.  In fact I tried CloseFile, but got a "File not open" error.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

Handoko

  • Hero Member
  • *****
  • Posts: 5153
  • My goal: build my own game engine using Lazarus
Re: TStringList.SaveToFile error
« Reply #9 on: September 05, 2018, 04:31:03 am »
Can you provide the whole compilable source code? If for some reasons you're not willing to publicize the project, you can create a compilable demo that can show the problem.

Create a new folder, copy and paste all the necessary files to it. Except: *.bak, the binary, lib and backup folders. Then compress the folder and send the zip file to the forum.

My guess, the bug is not located at the source code you already showed us.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TStringList.SaveToFile error
« Reply #10 on: September 05, 2018, 05:19:58 am »
Sounds like relativity error. show us the filenames
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: TStringList.SaveToFile error
« Reply #11 on: September 05, 2018, 04:57:17 pm »
     Handoko: The whole project is many thousands of lines long now, most of it irrelevant to this problem.  I can try to put together a project using this unit.  It could take a while, though.  Not sure I can start right away.
     taazz: Sorry, I don't know what "relativity error" means.  But there's nothing unusual about the file names.  "Test123.csp", "Cpds and Ions.csp", "Elements Only - Copy(2).csp", etc.  These are all simple text files in directory "C:\lazarus\Projects\ChemSubs\Data\".
Lazarus 1.8.0; fpc 3.0.4; Windows 10

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TStringList.SaveToFile error
« Reply #12 on: September 05, 2018, 05:46:51 pm »
     Handoko: The whole project is many thousands of lines long now, most of it irrelevant to this problem.  I can try to put together a project using this unit.  It could take a while, though.  Not sure I can start right away.
     taazz: Sorry, I don't know what "relativity error" means.  But there's nothing unusual about the file names.  "Test123.csp", "Cpds and Ions.csp", "Elements Only - Copy(2).csp", etc.  These are all simple text files in directory "C:\lazarus\Projects\ChemSubs\Data\".
Relativity means that the filename does not include the exact path in it. eg
Code: Pascal  [Select][+][-]
  1. stringlist.Loadfromfile('Test123.csp');
means add the current directory to the filename Test123.csp and try to open that file. Since the current directory is a OS global variable it can be changed by any program at any time, that makes your filename unsafe to use, try defining the complete and exact filename eg
Code: Pascal  [Select][+][-]
  1. stringlist.Loadfromfile('C:\lazarus\Projects\ChemSubs\Data\Test123.csp')
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: TStringList.SaveToFile error
« Reply #13 on: September 05, 2018, 06:04:55 pm »
     OK. Thanks. Makes sense now. In fact that's what the function FullCSPFileName does.  It makes sure the file name has the correct directory and extension.  It is used throughout the program and always works properly, including in the code above.  (I put  "FileName:=FullCSPFileName(FileList);"  immediately before the call to SaveToFile and it gives the correct result.)
(Sorry. Can't seem to get rid of the italics.)
« Last Edit: September 05, 2018, 06:07:37 pm by William Marshall »
Lazarus 1.8.0; fpc 3.0.4; Windows 10

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TStringList.SaveToFile error
« Reply #14 on: September 05, 2018, 07:49:26 pm »
     OK. Thanks. Makes sense now. In fact that's what the function FullCSPFileName does.  It makes sure the file name has the correct directory and extension.  It is used throughout the program and always works properly, including in the code above.  (I put  "FileName:=FullCSPFileName(FileList);"  immediately before the call to SaveToFile and it gives the correct result.)
(Sorry. Can't seem to get rid of the italics.)
The only thing left to do is to create a minimal example application that demonstrates the problem if that does not help you to identify it post the example here for us to take a look.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018