Recent

Author Topic: Access Violation on TMemoryStream  (Read 8460 times)

captian jaster

  • Guest
Access Violation on TMemoryStream
« on: October 29, 2010, 04:07:34 pm »
Code: Pascal  [Select][+][-]
  1. program MemStreamRead;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,SysUtils;
  7.  
  8. var
  9.  FS:TMemoryStream;
  10.  Buf:String = '';
  11.  
  12. begin
  13.   Writeln('Trying to read the stream');
  14.   FS := TMemoryStream.Create;
  15.   FS.LoadFromFile('Mem.txt');
  16.   Try
  17.    FS.Read(Buf,SizeOf(String));
  18.    Writeln(Buf);
  19.   Except
  20.    on E:Exception Do
  21.     Writeln('Error: '+E.Message);
  22.   end;
  23.   Readln;
  24. end.
  25.                  
  26.  
Whats wrong?
I'm trying to read a simple string from a file.

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Access Violation on TMemoryStream
« Reply #1 on: October 29, 2010, 04:17:06 pm »
A 'string' is not a buffer with an unlimited size that you can access directly.

Try something like this:
Code: Pascal  [Select][+][-]
  1. //...
  2.   FS.LoadFromFile('Mem.txt');
  3.   Try
  4.    SetLength(buf,FS.Size);
  5.    FS.Read(Buf[1],FS.Size);
  6.    Writeln(Buf);
  7. //..

CJ: BTW: why didn't you use your favourite old-skool procs?  :)

Code: Pascal  [Select][+][-]
  1.   AssignFile('mem.txt',T);  
  2.   reset(T);  
  3.   readln(T,buf);
  4.   CloseFile(T);  
« Last Edit: October 29, 2010, 04:22:54 pm by eny »
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

captian jaster

  • Guest
Re: Access Violation on TMemoryStream
« Reply #2 on: October 29, 2010, 04:34:51 pm »
A 'string' is not a buffer with an unlimited size that you can access directly.

Try something like this:
Code: Pascal  [Select][+][-]
  1. //...
  2.   FS.LoadFromFile('Mem.txt');
  3.   Try
  4.    SetLength(buf,FS.Size);
  5.    FS.Read(Buf[1],FS.Size);
  6.    Writeln(Buf);
  7. //..

CJ: BTW: why didn't you use your favourite old-skool procs?  :)

Code: Pascal  [Select][+][-]
  1.   AssignFile('mem.txt',T);  
  2.   reset(T);  
  3.   readln(T,buf);
  4.   CloseFile(T);  
;D Not my favorite. Just generally what I tell other people to start with.  It does read the string just not like I was hoping  :(  .... How do I convert it back to a regular string?.. Also Favorite

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Access Violation on TMemoryStream
« Reply #3 on: October 29, 2010, 08:14:10 pm »
It does read the string just not like I was hoping  :(
Programming and hoping are a bad combination.

Quote
How do I convert it back to a regular string?
What do you consider a 'regular string'?

Quote
.. Also Favorite
The world's a big place  8-)
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

captian jaster

  • Guest
Re: Access Violation on TMemoryStream
« Reply #4 on: October 30, 2010, 04:34:08 am »
It does read the string just not like I was hoping  :(
Programming and hoping are a bad combination.

Quote
How do I convert it back to a regular string?
What do you consider a 'regular string'?

Quote
.. Also Favorite
The world's a big place  8-)
XD Ok Cool..
My Idea was I wrote Hello World into the filestream with one Program and saved it and now I'm reading it back... So... Yeah....

 

TinyPortal © 2005-2018