 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
AC Slater Guest
|
Posted: Sat Jan 24, 2004 3:10 pm Post subject: Simple C++ ifstream question |
|
|
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
|
Posted: Sun Jan 25, 2004 1:33 am Post subject: Re: Simple C++ ifstream question |
|
|
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
|
Posted: Sun Jan 25, 2004 1:35 am Post subject: Re: Simple C++ ifstream question |
|
|
"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
|
Posted: Sun Jan 25, 2004 10:44 am Post subject: Re: Simple C++ ifstream question |
|
|
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 |
|
 |
|
|
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
|
|