I have a problem reading a plain csv file, generated with Excel 2013. It seems the encoding is not working correctly within the TStreamReader class. The strange thing is, one file is working the other one not. When reading the second file, TStreamReader returns an empty string:
LString := FEncoding.GetString(LBuffer, StartIndex, ByteBufLen);
Both files have the 1 Byte ANSI encoding. But TStreamReader is using UTF8 encoding.
My code:
fs := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyNone);
sr := TStreamReader.Create(fs);
while (not sr.EndOfStream) do //sr.EndOfStream is always true!!!!
begin
//some code here
end;
So far I figured out, that the following function is returning an empty string:
function TMBCSEncoding.GetCharCount(Bytes: PByte; ByteCount: Integer): Integer;
begin
Result := UnicodeFromLocaleChars(FCodePage, FMBToWCharFlags,
PAnsiChar(Bytes), ByteCount, nil, 0);
end;
When I compare both files, they have the same inputs beside the Bytes and ByteCount variable. But the Bytes startes with same values (same csv header names).
So my question is, why is one file working and the other not? What can I do to read the files correctly?