🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4986

Re: char *buf is bugged ?

#4986
From: Antonio Felix
Date: Mon, 24 Oct 2005 13:01
31 lines
1902 bytes
"Alfred" <alf@no.spam> wrote:
>
>Hello, I have a problem. I am writing a routing to read a file and write it into a Memo. I am using char *buf as buffer to read It becouse I need to change some parts before print it. The file I read is just a simple : "1" with no "" , so only 1 char. The output is : "1��" with no "".. what is wrong ? I have problems with only some File->Size and it work perfect with others File-Size.... I dont understand why...
>
>void __fastcall TForm1::Button1Click(TObject *Sender)
>{
>        TFileStream *File=new
>        TFileStream("list.txt",fmOpenRead);
>        int Size=File->Size;
>        char *buf = (char *) calloc( Size, sizeof(char) );
>        File->Read(buf,Size);
>        Form1->Memo->Lines->Add(buf);
>        File->Free();
>        free( buf );
>}
>Thank you
>Alfred


Hi Alfred,
The char *buf expects a null terminated string!
Try with:
   ...
   char *buf = (char *) calloc( Size + 1, sizeof(char) );
   File->Read(buf,Size);
   Form1->Memo->Lines->Add(buf);
   ...

HTH
Antonio
Technical Details
Message-ID: <435cdb11$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <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: "Antonio Felix" <nomail@nm.pt>
Sender: "Antonio Felix" <nomail@nm.pt>
Reply-To: "Antonio Felix" <nomail@nm.pt>
Subject: Re: char *buf is bugged ?
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 195.245.190.93     
X-Forwarded-For: unknown
References:  <435cd85e$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <435cdb11$1@newsgroups.borland.com>
Date: 24 Oct 2005 06:01:05 -0700
X-Trace: newsgroups.borland.com 1130158865 207.105.83.66 (24 Oct 2005 06:01:05 -0700)
Lines: 31
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail