Recent

Author Topic: [Solved] Directory and file access, robust, multiplatform functionality?  (Read 1978 times)

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Newbie question: I'm looking for a robust and multiplatform functionality for handling file access.

Basically I would like the functionality to make file access less low level and provide:

Does file already exist? (flag must hold this info, say FileAlreadyExists).
If file doesn't exist, try to create it.
Flag if creation was successful or not (FileCreatedSuccessfully)
Flag if file is readable or not (FileReadable).
Flag if file is writable or not (FileWritable).
And off course enable writing and reading strings to/from file (one line at a time) (ReadL, WriteL) with proper indications of success or failure.

Target is to handle log files (text) and csv files (text), so basically text files in both cases (utf8 formatted). Attached is an attempt that doesn't seem to work (log file never gets created)?
Later I discovered http://wiki.freepascal.org/File_Handling_In_Pascal (I assume it is up to date) but, as mentioned above, I would like a little extra functionality on top of this. I guess next step is to give TFilestream an attempt, but first I would like to understand why attached doesn't work?

Regards,
Michael
« Last Edit: April 04, 2018, 08:10:21 pm by mijen67 »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Directory and file access, robust, multiplatform functionality?
« Reply #1 on: October 19, 2017, 03:23:12 pm »
Code: Pascal  [Select][+][-]
  1.   if (DirSucc AND DirIsNew AND TryCreate) then
  2.     begin
  3.       // FileH:=FileCreateUTF8(ConfigFileWithPath);
  4.       FileH:=FileOpen(ConfigFileWithPath, fmOpenReadWrite);
  5.       if FileH=-1 then
  6.         begin
  7.           FileSucc:=false;
  8.           WriteLn('AppData: File creation not successfull, config file')
  9.         end
  10.       else
  11.         begin
  12.           WriteLn('AppData: File creation successfull, config file');
  13.           FileSucc:=true;
  14.           ConfigFileIsNew:=true;
  15.           FileClose(FileH)
  16.         end
  17.     end;
  18.  
since the dir is new the file does nt exist by default you need to use filecreate not fileopen
and the rest of the code has the same error as far as I can see but I'm a firm supporter of the TStream hierarchy so please use a tfilestream.
EDIT:
Oh I forgot to add that you do not check if your directory variable ends with a path delimiter which might lead to wrong file names use the function includetrailingpathdelimeter on your directory variable before concatenating with your filename and keep in mind that in most function that manipulate extensions the dot "." is considered part of the extension for comfort reasons I guess.
« Last Edit: October 19, 2017, 03:33:51 pm by taazz »
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

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Directory and file access, robust, multiplatform functionality?
« Reply #2 on: October 29, 2017, 06:48:52 pm »
since the dir is new the file does nt exist by default you need to use filecreate not fileopen

Got it, thanks!

And very usefull hint with the check of path delimiter, thanks!

 

TinyPortal © 2005-2018