Board index » cppbuilder » fgets() equivalent

fgets() equivalent


2007-11-27 04:46:51 PM
cppbuilder110
Is there a fgets() equivalent in
TFileStream class? I want to read from
a file till a newline character is found.
I do not know the size of the buffer.
-Harish
 
 

Re:fgets() equivalent

Hi Harish
Harish says:
Quote

Is there a fgets() equivalent in
TFileStream class? I want to read from
a file till a newline character is found.
I do not know the size of the buffer.
-Harish
If it is lines of text You wat to read just use
a TStringList
TStringList *StrList = new TStringList();
StrList->LoadFromFile("FileName");
//Then You can loop through the lines of text
//All the work of reading the file is done for You
delete StrList;
P.s. You always know the size of a TFileStream
it is in the TFileStream->Size property
Kind regards
Asger
 

Re:fgets() equivalent

Quote
If it is lines of text You wat to read just use
a TStringList
Thanks for the tip.
TStringList looks OK for a small number
of strings, but I am going to read a huge
file having several thousands of lines.
I would be better if I load one line at a time,
thus saving memory.
-Regards,Harish
 

{smallsort}

Re:fgets() equivalent

Hi Harish
Harish says:
Quote

>If it is lines of text You wat to read just use
>a TStringList

Thanks for the tip.
TStringList looks OK for a small number
of strings, but I am going to read a huge
file having several thousands of lines.
I would be better if I load one line at a time,
thus saving memory.
-Regards,Harish
Then You have the STL fstream which have a getline
(all the functions are mentioned in basic_istream)
but there is nothing wrong by using fgets that still
works perfectly well.
Kind regards
Asger
 

Re:fgets() equivalent

"Harish" < XXXX@XXXXX.COM >wrote in message
Quote
Is there a fgets() equivalent in TFileStream class?
No.
Quote
I want to read from a file till a newline character is found.
You will have to read from the stream 1 byte at a time manually, ie:
AnsiString __fastcall ReadLineFromStream(TStream *Stream)
{
AnsiString Result;
char c;
do
{
if( Stream->Read(&c, 1) != 1 )
break;
if( c == '\n' )
{
if( (Result.Length()>0) && (Result[Result.Length()] ==
'\r') )
Result.SetLength(Result.Length()-1);
break;
}
Result += c;
}
while( true );
return Result;
}
One way to better optimize the reading would be to implement your own
buffering, such as by deriving a new TStream class that overrides the
virtual Read() method, ie:
class TBufferedReadFileStream : public TFileStream
{
private:
BYTE FDataBuffer[1024];
int FNumInBuffer;
int FDataPos;
public:
__fastcall TBufferedReadFileStream(const AnsiString &FileName);
virtual int __fastcall Read(void *Buffer, int Count);
};
__fastcall TBufferedReadFileStream::TBufferedReadFileStream(const
AnsiString &FileName)
: TFileStream(FileName, fmOpenRead | fmShareDenyWrite)
{
}
int __fastcall TBufferedReadFileStream::Read(void *Buffer, int Count)
{
if( FNumInBuffer < 1 )
{
FDataPos = 0;
FNumInBuffer = TFileStream::Read(FDataBuffer, 1024);
}
if( Count>FNumInBuffer )
Count = FNumInBuffer;
if( Count>0 )
{
memcpy(Buffer, &FDataBuffer[FDataPos], Count);
FDataPos += Count;
FNumInBuffer -= Count;
}
return Count;
}
Gambit