Recent

Author Topic: TCSVDataset.LoadFromCSVStream  (Read 1535 times)

mcculloch

  • New Member
  • *
  • Posts: 23
TCSVDataset.LoadFromCSVStream
« on: July 22, 2025, 03:23:54 pm »
I'm trying to use TCSVDataset.LoadFromCSVStream to load a grid from a TFileStream.   It works to load data into the grid.  However it starts reading at the beginning of the stream instead of the current position of the stream.  That is the documented behaviour, but not what I want.  I was wondering if I could create a TStream of some kind that would start at the current position of my TFileStream.  If I can't, I have to do something totally different.  It would be nice if LoadFromCSVStream had an option NOT to reset the stream to the beginning, but it doesn't.

Is there a way to create a new TStream descendent that starts at the current position of an existing stream?

Thanks

wp

  • Hero Member
  • *****
  • Posts: 13491
Re: TCSVDataset.LoadFromCSVStream
« Reply #1 on: July 22, 2025, 04:14:56 pm »
For reading the file into into a stringgrid you could use the TStringGrid method LoadFromCSVStream (or LoadFromCSVFile) directly, bypassing the TCSVDataset. TStringGrid.LoadFromCSVStream does not reposition the stream to its beginning, but starts reading from the current position.

In case of your original question, reading from a stream into a TCSVDataset from the current stream position, you could read everything which follows the current stream position into a buffer string variable which you then can use the create a TStringStream (untested):

Code: Pascal  [Select][+][-]
  1. var
  2.   buffer: String;
  3.   strStream: TStringStream;
  4. begin
  5.   SetLength(buffer, AStream.Size - AStream.Position);
  6.   AStream.Read(buffer[1], Length(buffer));
  7.   strStream := TStringStream.Create(buffer);
  8.   try
  9.     CSVDataset.LoadFromStream(strStream);
  10.     ...
  11.   finally
  12.     strStream.Free;
  13.   end;

mcculloch

  • New Member
  • *
  • Posts: 23
Re: TCSVDataset.LoadFromCSVStream
« Reply #2 on: July 22, 2025, 11:57:31 pm »
Thanks!  I think I'll try the first option, but the TStreamString option looks an opportunity for a future project.  I may make unit that generalizes that approach, maybe in the form of class inheriting TStringStream.

mcculloch

  • New Member
  • *
  • Posts: 23
Re: TCSVDataset.LoadFromCSVStream
« Reply #3 on: July 23, 2025, 02:49:31 pm »
TStringGrid.LoadFromCSVStream works well.  It even has a FromLine argument that makes using a TStringStream unnecessary.  I don't know if loading into a TCSVDataset has that or not.

 

TinyPortal © 2005-2018