-3

I'm trying to write something to a text file in free pascal, but it doesn't write anything.

Here is my code:

program start;

uses crt;

type txt = file of string;

var 
    h : string;
    f : txt;

begin
    assign(f,'hey.txt');
    rewrite(f);
    h := 'jmhtdjh';
    write(f,h);
    close(f);
    readkey;
end.
huck dupr
  • 36
  • 5
  • @lurker: Thanks a lot **lurker**, I fixed it but it still dosen't work. – huck dupr Jun 23 '20 at 11:44
  • That's odd. I can compile your program, run it, and `hey.txt` appears in the current folder with the contents "jmhtdjh". There's no line feed at the end, so make sure you check the front of your command prompt as it will be all smooshed together. – lurker Jun 23 '20 at 12:44

1 Answers1

4

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.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • In a console app sometimes you really don't want to specify the full path – David Heffernan Jun 22 '20 at 18:58
  • @DavidHeffernan: Thanks. I don't personally recollect ever having a reason not to but I'm sure you're right so I've changed it a bit. – MartynA Jun 22 '20 at 19:16
  • There are huge numbers of examples. Pretty much any console app that accepts filenames as arguments. If you've ever used such a tool, and I am sure you have, then you will have just typed in the filename without the path. – David Heffernan Jun 22 '20 at 21:44
  • @DavidHeffernan: Oh, ok, I get what you mean now. – MartynA Jun 22 '20 at 21:47
  • I would say that you never rely of current directory in a GUI app because it is seldom well defined. However, in a console app the current directory is invariably well defined and I'd consider it to be an input to the program. That's how I think of it. – David Heffernan Jun 22 '20 at 21:50
  • I tried to enter the path but it still dosen't work. – huck dupr Jun 23 '20 at 11:49
  • See rewritten answer. – MartynA Jun 23 '20 at 15:30
  • 1
    @huckdupr "Doesn't work" is a poor description of a problem. Is the file created? What size (in bytes) is the file? If file size > 0 what does it contain when you look at it in a hex editor? Did you get any error messages? As you see, there are plenty of details that may shed some more light on the actual problem, instead of a dull "doesn't work" – Tom Brunberg Jun 23 '20 at 20:23
  • Thanks @TomBrunberg. I confess I'm slightly losing interest in trying to help if the OP won't even describe the symptoms. – MartynA Jun 23 '20 at 20:32
  • Yeah @MartynA I know the feeling. – Tom Brunberg Jun 23 '20 at 21:01
  • So, do you need any more help with this or not? – MartynA Jun 25 '20 at 06:29
  • @tombrunberg ,I know it's a poor description but there is no error, and the file already exists, but it doesn't change. – huck dupr Jun 25 '20 at 09:37
  • @huckdupr How do you know it doesn't change? I already asked you to tell us the content of the file, so what is it before and after you run your program? Does the time stamp of the file change? If you don't answer all requests for additional information about your problem, the interest to help you is soon evaporating in thin air. – Tom Brunberg Jun 25 '20 at 10:10
  • it is advisable to use text as file type instead of file of string when you are writing to plain text files ... so: var f : text – robert aleksic Jul 16 '20 at 21:24