Recent

Author Topic: Can't create text file using assign() [Solved]  (Read 1652 times)

BrokenMemory

  • New Member
  • *
  • Posts: 13
Can't create text file using assign() [Solved]
« on: August 26, 2020, 01:48:34 pm »
Greetings,I was handling my Computer Science project and this problem had me stopped for like half an hour,I wanted to create a text file for each time I click the button, however using assign() function didn't seem to work,here's the code.

procedure TForm1.Button1Click(Sender: TObject);
var f : text;
    fn : string;
begin
  memo1.Clear;
  fn := 'C:\SBA(ICT) 7_2\Fault Reports\Pending\' + datetimetostr(now) + '.txt';
  if Rm <> ''
  then if Eq <> ''
       then if Er <> ''
            then begin
                   Assignfile(f,fn);
                   rewrite(f);
                   showmessage('Creation successful');
                   writeln(f,Rm);
                   writeln(f,Eq);
                 end
            else showmessage('You have not select the fault yet!')
       else showmessage('You have not select the equipment yet!')
  else showmessage('You have not select the room yet!')
end;

P.S. I'm new to this forum,so if I violated some rules,be sure to let me know and it won't happen next time.
« Last Edit: August 26, 2020, 02:11:26 pm by BrokenMemory »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12593
  • FPC developer.
Re: Can't create text file using assign()
« Reply #1 on: August 26, 2020, 01:55:34 pm »
Note that all directories must exist for this to work. Assignfile won't create directories.

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: Can't create text file using assign()
« Reply #2 on: August 26, 2020, 01:55:47 pm »
Assuming this is in windows, datetimetostr() might insert illegal characters (slashes, colons).
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

BrokenMemory

  • New Member
  • *
  • Posts: 13
Re: Can't create text file using assign()
« Reply #3 on: August 26, 2020, 01:58:02 pm »
Note that all directories must exist for this to work. Assignfile won't create directories.

Got it, is there any way to do so then, have been searching for quite a while now.

BrokenMemory

  • New Member
  • *
  • Posts: 13
Re: Can't create text file using assign()
« Reply #4 on: August 26, 2020, 01:59:37 pm »
Assuming this is in windows, datetimetostr() might insert illegal characters (slashes, colons).

I have checked the variable fn, it doesn't seem to have any illegal characters.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12593
  • FPC developer.
Re: Can't create text file using assign()
« Reply #5 on: August 26, 2020, 01:59:56 pm »
Get the directory part in a string (e.g. "path") and then do a

Code: Pascal  [Select][+][-]
  1. ForceDirectories(Path);


Eny is right though to look into that , your program might fail on a different machine this way.


« Last Edit: August 26, 2020, 02:03:18 pm by marcov »

BrokenMemory

  • New Member
  • *
  • Posts: 13
Re: Can't create text file using assign()
« Reply #6 on: August 26, 2020, 02:06:08 pm »
Get the directory part in a string (e.g. "path") and then do a

Code: Pascal  [Select][+][-]
  1. ForceDirectories(Path);

I have added the forcedirectories() into the program, however, there is still a run time error during rewrite(f), and the file is still not created, here's the code. Apologies if I misunderstood anything.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var f : text;
  3.     fn : string;
  4. begin
  5.   memo1.Clear;
  6.   fn := 'C:\SBA(ICT) 7_2\Fault Reports\Pending\' + datetimetostr(now) + '.txt';
  7.   if Rm <> ''
  8.   then if Eq <> ''
  9.        then if Er <> ''
  10.             then begin
  11.                    Forcedirectories(fn);
  12.                    Assignfile(f,fn);
  13.                    rewrite(f);
  14.                    showmessage('Creation successful');
  15.                    writeln(f,Rm);
  16.                    writeln(f,Eq);
  17.                  end
  18.             else showmessage('You have not select the fault yet!')
  19.        else showmessage('You have not select the equipment yet!')
  20.   else showmessage('You have not select the room yet!')
  21. end;
  22.  

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Can't create text file using assign()
« Reply #7 on: August 26, 2020, 02:07:34 pm »
Hi!

As said above: DateTimeToStr contains slashes and colons.
Both are forbidden in a filename.
Use hyphens or underscores instead.

Replace the DateTimeToStr with

Code: Pascal  [Select][+][-]
  1. FormatDateTime('yyyy-mm-dd hh-nn-ss',now)


Winni

BrokenMemory

  • New Member
  • *
  • Posts: 13
Re: Can't create text file using assign()
« Reply #8 on: August 26, 2020, 02:12:26 pm »
Hi!

As said above: DateTimeToStr contains slashes and colons.
Both are forbidden in a filename.
Use hyphens or underscores instead.

Replace the DateTimeToStr with

Code: Pascal  [Select][+][-]
  1. FormatDateTime('yyyy-mm-dd hh-nn-ss',now)


Winni

This method works,thank you very much.

dpremus

  • New Member
  • *
  • Posts: 32
Re: Can't create text file using assign() [Solved]
« Reply #9 on: August 26, 2020, 02:44:29 pm »
Your code is very hard to read, here is the same code written in another way and it's more readable.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.    f  : text;
  4.    fn : string;
  5. begin  
  6.   if Rm = '' then begin
  7.     showmessage('You have not select the room yet!');
  8.     exit
  9.   end;
  10.  
  11.   if Eq = '' then begin
  12.     showmessage('You have not select the equipment yet!');
  13.     exit
  14.   end;
  15.  
  16.   if Er = '' then begin
  17.     showmessage('You have not select the fault yet!');
  18.     exit
  19.   end;  
  20.  
  21.   memo1.Clear;
  22.   fn := 'C:\SBA(ICT) 7_2\Fault Reports\Pending\' + FormatDateTime('yyyy-mm-dd hh-nn-ss', now) + '.txt';
  23.   Assignfile(f,fn);
  24.   rewrite(f);
  25.   showmessage('Creation successful');
  26.   writeln(f,Rm);
  27.   writeln(f,Eq);
  28.   closefile(f);   // <- missing in original code
  29. end;

 

TinyPortal © 2005-2018