Recent

Author Topic: File creation error in Windows, Linux is OK  (Read 3267 times)

edvard

  • Full Member
  • ***
  • Posts: 172
File creation error in Windows, Linux is OK
« on: December 28, 2014, 03:07:52 am »
OK, I finally got a project mostly done, so I'm preparing to release (I'll announce it on this board so you all can flame critique my programming skills).

I have a bit of a strange error on Windows, however.  Take this piece of code, which writes a new configuration file if none is found
(PBConfig is defined as a TStringlist, and freed later in the code, CFGFile is a function that returns the result of GetAppConfigFile(false)):
Code: [Select]
procedure makeConfig;
begin
  PBConfig := TStringList.Create;
  PBConfig.Add('UserKey=');
  PBConfig.Add('Format=0');
  PBConfig.Add('Expire=0');
  PBConfig.Add('Private=0');
  PBConfig.SaveToFile(CFGFile);
end;

If I leave it as-is, I get a "Project pastequick raised exception class 'EFCreateError' with message:  Unable to create file "C:\users\me\AppData\local\pastequick\pastequick.cfg""
No spaces in the path, so I couldn't really figure that out, then I thought "Maybe the error is due to it trying to write to a file that doesn't exist, so why don't I create the file first with FileCreate?"
Great idea, says /me
Code: [Select]
procedure makeConfig;
begin
  FileCreate(CFGFile);
 ...

Same error, so now I do this:
Code: [Select]
procedure makeConfig;
begin
  ForceDirectories(GetAppConfigDir(false));
  FileCreate(CFGFile);
 ...

Now it's giving me an error that it can't read the file, but the file is created.  The file that is created  is 0 kb. ???
A second compile attempt gives the error:
"project pastequick raised exception class 'EConvertError' with message: "" is an invalid integer"

WTH? %)
« Last Edit: December 28, 2014, 03:21:17 am by edvard »
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

sapper

  • New Member
  • *
  • Posts: 35
Re: File creation error in Windows, Linux is OK
« Reply #1 on: December 28, 2014, 06:25:45 am »
You are on the right track.

The subdirectory
Quote
\pastequick
needs to be created.

This test code worked for me.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
   PBConfig : TStringList;
   path : string;
begin
  PBConfig := TStringList.Create;
  PBConfig.Add('UserKey=');
  PBConfig.Add('Format=0');
  PBConfig.Add('Expire=0');
  PBConfig.Add('Private=0');
  path := GetAppConfigFile(False);
  if ForceDirectories(ExtractFilePath(path)) then
     PBConfig.SaveToFile(path)
  else
      ShowMessage('Unable to create path');
  PBConfig.Free;
end;   
       

EDIT: If using FileCreate you should probably use the THandle returned to close the file with FileClose before attempting to write/read since I assume you are not using FileWrite or FileRead.
« Last Edit: December 28, 2014, 06:44:56 am by sapper »

edvard

  • Full Member
  • ***
  • Posts: 172
Re: File creation error in Windows, Linux is OK
« Reply #2 on: December 28, 2014, 09:13:59 am »
Thanks! That works great.  I'll credit you in the source, and I'll post about my project tomorrow.  I'm going to bed now...
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

 

TinyPortal © 2005-2018