Lazarus

Programming => General => Topic started by: varg300 on May 07, 2012, 09:11:46 pm

Title: [solved] Saving a datarecord: Error: Typed files cannot contain reference...
Post by: varg300 on May 07, 2012, 09:11:46 pm
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?
Title: Re: Saving a datarecord: Error: Typed files cannot contain reference...
Post by: Pascaluvr on May 08, 2012, 12:17:10 am
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];

Title: Re: Saving a datarecord: Error: Typed files cannot contain reference...
Post by: varg300 on May 08, 2012, 04:29:39 pm
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
Title: Re: Saving a datarecord: Error: Typed files cannot contain reference...
Post by: KpjComp on May 08, 2012, 04:49:28 pm
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