Article View: borland.public.cppbuilder.nativeapi
Article #4991Re: char *buf is bugged ?
From: Remy Lebeau (TeamB)
Date: Mon, 24 Oct 2005 17:52
Date: Mon, 24 Oct 2005 17:52
34 lines
1859 bytes
1859 bytes
"Alfred" <alf@no.spam> wrote in message
news:435cd85e$1@newsgroups.borland.com...
> The file I read is just a simple : "1" with no "" , so only 1 char.
So then why are you dynamically allocating a 1-byte buffer at all? You can
use a single char variable for that instead, ie:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TFileStream *File = new TFileStream("list.txt", fmOpenRead |
fmShareDenyWrite);
try
{
if( File->Size == 1 )
{
char buf;
File->Read(&buf, 1);
Memo->Lines->Add(buf);
}
}
__finally {
delete File; // <-- never call Free() directly.
}
}
> The output is : "1f�" with no "".. what is wrong ?
Your buffer does not have a null terminator, so the Add() ends up reading
into surrounding memory.
Gambit
Technical Details
Message-ID:
<435d1eac$3@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.moat.net!zen.net.uk!dedekind.zen.co.uk!news-out.usenet.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<435cd85e$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,a803499fd1f61208 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <435cd85e$1@newsgroups.borland.com> Subject: Re: char *buf is bugged ? Date: Mon, 24 Oct 2005 10:52:03 -0700 Lines: 36 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <435d1eac$3@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1130176172 68.69.17.41 (24 Oct 2005 10:49:32 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.moat.net!zen.net.uk!dedekind.zen.co.uk!news-out.usenet.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail