C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple C++ ifstream question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
AC Slater
Guest





PostPosted: Sat Jan 24, 2004 3:10 pm    Post subject: Simple C++ ifstream question Reply with quote



In regards to the following code:

char tmp[6];

myifstread.read(tmp,5);

Does tmp[5] = '' by definition? E.g. does .read put the null terminator?
If not, why would it be that for months the print statement after the read
would show just 5 characters than out of nowhere sometimes it shows 5 chars
and then some garbage?

Just trying to understand whats going on here.

Frank



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeff Schwab
Guest





PostPosted: Sun Jan 25, 2004 1:33 am    Post subject: Re: Simple C++ ifstream question Reply with quote



AC Slater wrote:
Quote:
In regards to the following code:

char tmp[6];

myifstread.read(tmp,5);

Does tmp[5] = '' by definition? E.g. does .read put the null terminator?

Of course not. What if your buffer were only 5 characters long?

Quote:
If not, why would it be that for months the print statement after the read
would show just 5 characters than out of nowhere sometimes it shows 5 chars
and then some garbage?

You were unlucky. The array of characters may have started with ''
values.

Quote:
Just trying to understand whats going on here.

Frank

If you want the old behavior back, initialize the array explicitly.

char tmp[ 6 ] = { '' };


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Sun Jan 25, 2004 1:35 am    Post subject: Re: Simple C++ ifstream question Reply with quote



"AC Slater" <notme (AT) yahoo (DOT) com> wrote

Quote:
In regards to the following code:

char tmp[6];

myifstread.read(tmp,5);

Does tmp[5] = '' by definition? E.g. does .read put the null
terminator?


There would be no reason to insert a null character at index 5. Often
when one invokes read one does not read an entire string, but just
part of a string or pure binary data. Furthermore, there is no way for
the stream to know how large the character buffer actually is. For all
it knows, it could have been called in this context:

char tmp[5];
myifstread.read(tmp,5);

Here, adding a terminating null character would result in underfined
behavior.

Quote:
If not, why would it be that for months the print statement after
the read
would show just 5 characters than out of nowhere sometimes it shows
5 chars
and then some garbage?

This behavior is perfectly consistent with the standard. Why it
happens I can't say for sure, but it may be that you are sometimes
using a static array, which is zero-initialized, or that your
implementation zero-initializes arrays in debug mode.

Jonathan





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Sun Jan 25, 2004 10:44 am    Post subject: Re: Simple C++ ifstream question Reply with quote

On 24 Jan 2004 10:10:08 -0500, AC Slater <notme (AT) yahoo (DOT) com> wrote:

Quote:
In regards to the following code:

char tmp[6];

myifstread.read(tmp,5);

Does tmp[5] = '' by definition?

No. The read stores exactly the number of characters that you direct.
Tmp[5] has whatever garbage was there before the call of read.

Quote:
E.g. does .read put the null terminator?

Absolutely not. Think about
int x;
myifstream.read((char*)&x, sizeof(x));
Read is an unformatted function.

Quote:
If not, why would it be that for months the print statement after the read
would show just 5 characters than out of nowhere sometimes it shows 5 chars
and then some garbage?

You were unlucky enough to have some random zeores in memory. It
usually works that way until you demo for your most important customer.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.