Recent

Author Topic: [Solved]How to write to sub-directory  (Read 1963 times)

Robert.Thompson

  • Jr. Member
  • **
  • Posts: 56
  • "A very bad coder."
    • Google Voice for Canadians
[Solved]How to write to sub-directory
« on: September 08, 2017, 05:59:16 pm »
Hello:

Using Lazarus 1.6.4  FPC 3.0.2 on LinuxMint 18.2

The following does not work as I hoped.

It creates the sub-directory (My_Files) within the working directory but it does not create the files in it – it creates the files in the working directory.


// create all output files required.
procedure CreateOutPutFiles;
begin
 
    // Create new sub-directory called: 'My_Files' for text file output.
    CreateDir('My_Files');
    // Create 'Placed_Calls.txt' file in sub-directory
    AssignFile(Placed,'My_Files\Placed_Calls.txt');
    //empty it.
    Rewrite(Placed);
    //Open it.
    Append(Placed);               
<snip> there are many more files created.

Does anyone know what I am doing wrong?

Thanks,

Rob.
« Last Edit: September 08, 2017, 06:51:35 pm by Robert.Thompson »
Lazarus:  1.8.4  2018-11-17
FPC:   3.0.4 x86_64-linux-gtk2
System:   Kernel: 4.15.0-39-generic x86_64 bits: 64 gcc: 7.3.0 Cinnamon 3.8.9 Linux Mint 19 Tara
              Phoenix v: 11JB.M044.20100622.hkk date: 06/22/2010
Intel Core i5 M 460 (-MT-MCP-) arch: Nehalem rev.5 cache: 3072 KB
NVIDIA GeForce 310M

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: How to write to sub-directory
« Reply #1 on: September 08, 2017, 06:19:16 pm »
'My_Files\Placed_Calls.txt'
First thing comes to my mind, you should use pathdelim to propely specify a sub-directory in a cross-platform (and a safe) way:
Code: Pascal  [Select][+][-]
  1. 'My_Files'+pathdelim+'Placed_Calls.txt'
Second, when you Rewrite file, you open it too.
Code: Pascal  [Select][+][-]
  1.     //empty it.
  2.     Rewrite(Placed); // <------ this is absolutely enough
    //Open it.
    Append(Placed);
   <----- I can hardly tell, why aren't you getting an IO error here.
And don't forget to CloseFile(Placed) after you've processed it, or you'll get data not written completely or an IO error.

(UPD) One more thing to check:
You may get a strange behaviour of CreateDir('My_Files'); in case the directory already exists. You should either delete it first or check if it exists and not create it again in such case.
« Last Edit: September 08, 2017, 06:21:45 pm by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Robert.Thompson

  • Jr. Member
  • **
  • Posts: 56
  • "A very bad coder."
    • Google Voice for Canadians
Re: How to write to sub-directory
« Reply #2 on: September 08, 2017, 06:50:57 pm »
Thank you Eugene! :)

Using +pathdelim+ works perfectly, and, deleting the Append(Placed) worked as well.

Thanks again,

Rob.
Lazarus:  1.8.4  2018-11-17
FPC:   3.0.4 x86_64-linux-gtk2
System:   Kernel: 4.15.0-39-generic x86_64 bits: 64 gcc: 7.3.0 Cinnamon 3.8.9 Linux Mint 19 Tara
              Phoenix v: 11JB.M044.20100622.hkk date: 06/22/2010
Intel Core i5 M 460 (-MT-MCP-) arch: Nehalem rev.5 cache: 3072 KB
NVIDIA GeForce 310M

 

TinyPortal © 2005-2018