Before I explain about your code, please compile and run the following program:
program TextFileCreate;
var
F : Text;
begin
Assign(F, 'C:\temp\test.txt');
Rewrite(F);
Writeln(F, 'testing ...');
Close(F);
Readln;
end.
That compiles and runs fine on Windows 10 and you can confirm that it does
by using Notepad to open the file. It uses the built-in Text
file type which is specifically
for working with .Txt files
Turning to your program, at first I thought your problem was just that you weren't
specifying the path where the file should be saved, so I suggested changing your Assign
statement to read
assign(f, 'C:\Temp\Hey.Txt');
However, when I tried to compile your code with that change, I got the error
FileOfString.lpr(7,26) Error: Typed files cannot contain reference-counted types.
. The reason for the error is that with my Lazarus's
default compiler settings, strings are so-called "huge strings" which can be up to 2Gb in
length and are reference counted, as the error message suggests. FPC supports two
types of strings, these "huge strings" and traditional Pascal "short strings", which
can be only up to 255 characters in length and are preceded by a "length byte" which
records how many bytes long the string is. You can switch between these string types using the {$H} compiler directive.
So I changed your code as shown below:
program FileOfString;
uses crt;
{$H-} // means use short strings
type txt = file of string;
var
h : string;
f : txt;
begin
assign(f,'c:\temp\hey.txt');
rewrite(f);
h := 'jmhtdjh';
write(f,h);
close(f);
readkey;
end.
This did compile and wrote the file hey.txt to the c:\temp folder and I could open it
with Notepad. However, the contents were probably not what you were expecting because
jmhtdjh
was preceded by the length-byte I mentioned, which is Chr(7), so in
Notepad the string starts with looks something like a hollow rectangle.
Frankly, it's not worth spending any time trying to "fix" this, just use the
supplied text
file type instead.