0

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?

7
  • The second argument of Rewrite should not be Firstname. Does it even compile? Also, you need to spend more time on chosing good variable names. Firstname is the path to the file containing the first name, and Fname is the first name. Not good. May 12, 2013 at 11:31
  • No, it is correct...even if i change it, the error will be raised May 12, 2013 at 11:32
  • No, it is definitely not correct. Trust me. A string as second argument of Rewrite doesn't make any sense. May 12, 2013 at 11:33
  • So i should just leave it as it is? F[1]? May 12, 2013 at 11:36
  • 1
    @user2296565, there is no point of having array of files unless you do some I/O on them on simultaneous basis.
    – OnTheFly
    May 12, 2013 at 13:47

1 Answer 1

1

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.

7
  • Because I....Have no idea about it, my teacher is speciallized in older versions of delphi and pascal, such as turbo pascal, and this is the only method i know...Where can i find the TstringList method? May 12, 2013 at 11:38
  • TStringList isn't a method. It's a class, and one that has always been very central to Delphi programming. For instance, the Items property of a TComboBox or TListBox, the Lines property of a TMemo or TRichEdit, and so on, are all TStringLists (essentially). So you have already used this class a lot in your previous questions! May 12, 2013 at 11:40
  • Ok thank you, Andreas...Can i also use this when writing in console application? May 12, 2013 at 11:59
  • @user2296565: You should know that old-school Pascal is deprecated, and shouldn't be used in new applications. There are a number of reasons for this. First, the FileMode is a global variable, and so two threads cannot have different modes. Second, Unicode support is somewhat awkward using Pascal I/O. May 12, 2013 at 11:59
  • @user2296565: Of course. TStringList can be used everywhere. May 12, 2013 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.