Recent

Author Topic: [solved] Saving a datarecord: Error: Typed files cannot contain reference...  (Read 12406 times)

varg300

  • New Member
  • *
  • Posts: 37
Im trying to save a datarecord (Variable-record?) into a file, but the compiler stops and gives me this error:
variablar.pas(22,24) Error: Typed files cannot contain reference-counted types.


This is the unit's code:
Code: [Select]
unit variablar;

{$mode objfpc}{$H+}

interface

uses  TextStrings,Classes, SysUtils;

type
data = Record
   ledig,inne : boolean;
   fnavn,enavn,plass,tlf : String;
   addr,merknad : TStrings;
   nummer : integer;
 end;

var
 nummer,i : integer;
 nykjel : array [1..100] OF data;
 datafil : File OF data;       //THIS IS WHERE IT STOPS

implementation

end.
This used to work in Turbo-Pascal, and the regular "text-mode FPC" as well, but not in Lazarus.
I'm guessing that there's a somewhat different approach to this in Lazarus?
« Last Edit: May 08, 2012, 07:47:32 pm by varg300 »

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
String(s) in FPC are pointers.  They are valid in a record, but not in a record used with File Of.  (if it were allowed, you would only be writing out a pointer which would probably point to an invalid address when you read it back in.

For strings to be used in a record that is to be written to a file you need to specify the (maximum) number of bytes in the string:

  Addr:    String[24];

Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

varg300

  • New Member
  • *
  • Posts: 37
Thanks, that might work.
What is the maximum string size allowed for File OF Record?
The record is using some TStrings as well, as i can understand they can hold several "lines" in one string, is that correct? Will they work with the datafile?

By the way, i've done some research and found that i might be better to use a TDataStream and PackedRecord instead of Record and FileOf Record, may that be a better way to do it?
I have no experience with using datastreams though, so i might need some explanation in how to do that

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Quote
What is the maximum string size allowed for File OF Record?

Doing string[size] basically is saying use shortstring.

Shortstring max size is 255.

 

TinyPortal © 2005-2018