Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-03

How to initialize a "Text" variable?

10 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Felipe Monteiro de Carvalho

Hello,

I have the following variable that I need to initialize:

yyinput: Text;

I searched, but I can't find anything that explains what this is, or how to initialize it. Is the Text type the same as TextFile?

I would like to set this variables to the contents of a file, so I tryed the following, but it crashes with runtime error 123 ( 'No medium found' ) and no error message.

  AssignFile(lexlib.yyinput, AFileName);
  Reset(lexlib.yyinput) ;
  fmlscan.yylex();
  CloseFile(lexlib.yyinput) ;

thanks,

Felipe Monteiro de Carvalho
Wanderlan Santos dos Anjos
to Felipe Monteiro de Carvalho

Hello Felipe,

Yes, Text is the same as TextFile.

Use:
Rewrite for create a new file for writing.
Reset for open a file already created for reading. Append for open a file already created for writing.

Att, Wanderlan Santos dos Anjos (061) 3345-5496; 9133-3804
Jonas Maebe
to Felipe Monteiro de Carvalho: I have the following variable that I need to initialize: yyinput: Text; I searched, but I…
Felipe Monteiro de Carvalho wrote 6 lines
I have the following variable that I need to initialize: yyinput: Text; I searched, but I can't find anything that explains what this is, or how to initialize it. Is the Text type the same as TextFile?

Yes (although more the other way around: Text is a standard Pascal type, while TextFile is an alias for Text introduced in Delphi).

Show quoted text 5 lines
I would like to set this variables to the contents of a file, so I tryed the following, but it crashes with runtime error 123 ( 'No medium found' ) and no error message. AssignFile(lexlib.yyinput, AFileName);

Normally, you would use plain assign() with Text files, but AssignFile maps through to that.

Show quoted text 5 lines
Reset(lexlib.yyinput) ; fmlscan.yylex(); CloseFile(lexlib.yyinput) ;

Same remark as for assign() here (close vs closefile)

So your error is unrelated to the use of assign(file)/close(file), it is something else.

Jonas

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho

Is it possible to set a Text file to nowhere? I mean, lexlib has a textfile called yyoutput, where it will throw output. I don't want to have this output at all, and lexlib doesn't allow me to choose that.

yyoutput is declared simply as Text

I would initially try setting it to zero, but that's StdIO.

thanks,

Felipe Monteiro de Carvalho
Michael Van Canneyt
to Felipe Monteiro de Carvalho: Is it possible to set a Text file to nowhere? I mean, lexlib has a textfile called…
Felipe Monteiro de Carvalho wrote 7 lines
Is it possible to set a Text file to nowhere? I mean, lexlib has a textfile called yyoutput, where it will throw output. I don't want to have this output at all, and lexlib doesn't allow me to choose that. yyoutput is declared simply as Text I would initially try setting it to zero, but that's StdIO.

Assign yyoutput to a dummy stream, which simply skips output.

Michael.

Michael Van Canneyt
to Felipe Monteiro de Carvalho: On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt Could you explain better how can…
Felipe Monteiro de Carvalho wrote 7 lines
On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt wrote: > Assign yyoutput to a dummy stream, which simply skips output. Could you explain better how can this be implemented? The only think I know about streams is TStream.

Exactly:

-----------------------------------------------------------------------

uses lexlib, Classes, StreamIO;
Type
  TNullStream = Class(TStream)
    function Read(var Buffer; Count : LongInt) : Longint; override;
    function Write(const Buffer; Count : LongInt) : LongInt; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
  end;
Function TNullStream.Read(var Buffer; Count : LongInt) : Longint;
begin
  Result:=Count;
end;
Function TNullStream.Write(const Buffer; Count : LongInt) : LongInt;
begin
  Result:=Count;
end;
function TNullStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
  Result:=0;
end;
Var
  Null : TNullStream;
begin
  Null:=TNullstream.Create;
  assignstream(yyoutput,Null);
  // blah blah
  FreeAndNil(Null);
end.
-----------------------------------------------------------------------

That's it. Come to think of it,I may add TNullStream to the streamex unit or so.

Michael.

Tomas Hajny
to Felipe Monteiro de Carvalho: On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt Could you explain better how can…
Felipe Monteiro de Carvalho wrote 7 lines
On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt wrote: > Assign yyoutput to a dummy stream, which simply skips output. Could you explain better how can this be implemented? The only think I know about streams is TStream.

I'm not sure if mixing lexlib (which is AFAIK written using procedural TP style code) with streams is a good idea. Depending on your platform, you could probably simply assign the text file name of 'NUL' or '/dev/nul'. Admittedly, I don't know whether this kind of device exists for all platforms (e.g. Symbian ;-) ).

Tomas

Sam Liddicott

Find the definition of the "text" type and see that it contains pointers to read and write functions.

They need replacing in yor instance to pointers to functions that do nothing.

The original Pascal file access is object oriented after all!

Sam

Earlier message 24 lines
-----Original Message----- From: Tomas Hajny Sent: 17 March 2008 07:17 To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] How to initialize a "Text" variable? On Sun, March 16, 2008 12:14, Felipe Monteiro de Carvalho wrote: > On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt > wrote: >> Assign yyoutput to a dummy stream, which simply skips output. > > Could you explain better how can this be implemented? > > The only think I know about streams is TStream. I'm not sure if mixing lexlib (which is AFAIK written using procedural TP style code) with streams is a good idea. Depending on your platform, you could probably simply assign the text file name of 'NUL' or '/dev/nul'. Admittedly, I don't know whether this kind of device exists for all platforms (e.g. Symbian ;-) ). Tomas