filedelphiassign

How to fix I/O error 102, When Assigning a file


My teacher assigned me to create a simple project: Create a form, which the user has to complete and when he presses the submit button, then the program should write all the information in different textfiles (For example, 1 textfile for their first name, onother for their last name etc.) (I know it sounds insane, but a project is a project). Anyway, I tried this:

procedure TForm1.Button1Click(Sender: TObject);   //On Click
var 
F:Array[1..20]of textfile;                        //20 fields, 1 txt each one
Firstname,Fname:string;                           //Location,Field

Begin
Firstname:=ExtractFilePath(Paramstr(0))+'User\Identity\FirstName.txt'; //Location
Fname:=Edit1.text;

AssignFile(f[1],Firstname);
Rewrite(f[1],Firstname);
writeln(f[1],Fname);
closefile(f[1]);
end;

I searched on the Google about this error:

I/O error 102,

All I found is that Delphi does not assign the file, for some reasons. But I still don't get why, the code looks absolutely correct and logical to me, Perhaps what I tried:

F:Array[1..20]of Textfile is wrong, the only reason i tried it, is because i wanted to start writing f1,f2,...,f20

What do you think I should do?


Solution

  • The second argument of Rewrite should not be a string. Does it even compile? There shouldn't be a second argument at all, IIRC. Do you have write access to the directory? Does the directory even exist? I don't think the RTL will create it for you. If this is indeed your problem, simply do

    ForceDirectories(ExtractFilePath(FileName)); // FileName=Firstname
    

    prior to calling Assign.