Hi,

This is what I am trying to do - If the filename is given I have to read
from the file else I have to read from cin. The program below compiles fine
but while reading from cin, it reads some part of the stream fine but
encounters an EOF prematuerly. If I read from the cin in a file I see the
full data. So I know that cin does get all the data, just that the read
function behaves in an unexpected way.

std::istream* myFile; // stream object to read the cin
std::filebuf* myFileBuf; // specifies a streamBuffer

.......

if (!myFileName)
{
myFile = &std::cin;
}
else
{
myFileBuf = new std::filebuf;
if (!(myFileBuf->open (myFileName, std::ios::in | std::ios::binary)))
{
status = My_FileOpenErr;
}
else
{
myFile = new std::istream (myFileBuf);
}
}

.........

Then I have the read function
if (!(myFile->read (reinterpret_cast<char*>(mydata), nBytes)))
{
if (myFile->eof())
{
status = My_EndOfFile;
}
else
{
status = My_IOErr;
}
}

Please help,
Thanks in advance.
M

Re: read from an istream object by Igor

Igor
Wed Sep 20 17:50:06 CDT 2006

manimau <manimau@discussions.microsoft.com> wrote:
> This is what I am trying to do - If the filename is given I have to
> read from the file else I have to read from cin. The program below
> compiles fine but while reading from cin, it reads some part of the
> stream fine but encounters an EOF prematuerly. If I read from the
> cin in a file I see the full data.

One difference is you open the file in binary mode, but cin is open in
text mode. In text mode, a character with code 26 (0x1A, Ctrl-Z)
indicates end-of-file. I bet your binary data just happens to have such
a byte.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: read from an istream object by manimau

manimau
Wed Sep 20 18:10:02 CDT 2006



"Igor Tandetnik" wrote:

> manimau <manimau@discussions.microsoft.com> wrote:
> > This is what I am trying to do - If the filename is given I have to
> > read from the file else I have to read from cin. The program below
> > compiles fine but while reading from cin, it reads some part of the
> > stream fine but encounters an EOF prematuerly. If I read from the
> > cin in a file I see the full data.
>
> One difference is you open the file in binary mode, but cin is open in
> text mode. In text mode, a character with code 26 (0x1A, Ctrl-Z)
> indicates end-of-file. I bet your binary data just happens to have such
> a byte.
> --
> With best wishes,
> Igor Tandetnik
>

Thank you so much for your reply... Though I never thought of that, but it
seems so very logical. So now my next quextion is how do I read in binary
mode from cin instead of in text mode?
I really appreciate your help.

Re: read from an istream object by Igor

Igor
Wed Sep 20 18:26:04 CDT 2006

manimau <manimau@discussions.microsoft.com> wrote:
> So now my next quextion is how do I
> read in binary mode from cin instead of in text mode?

I can't think of any portable way to do that. If you are happy with a
non-portable one, specific to Win32 and VC compiler, you can do the
following:

HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
int fd = _open_osfhandle((intptr_t)h, _O_BINARY);
FILE* f = _fdopen(fd, "rb");
fstream mycin(f);
// you can also construct a filebuf from FILE*

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: read from an istream object by manimau

manimau
Wed Sep 20 18:56:01 CDT 2006



"Igor Tandetnik" wrote:

> manimau <manimau@discussions.microsoft.com> wrote:
> > So now my next quextion is how do I
> > read in binary mode from cin instead of in text mode?
>
> I can't think of any portable way to do that. If you are happy with a
> non-portable one, specific to Win32 and VC compiler, you can do the
> following:
>
> HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
> int fd = _open_osfhandle((intptr_t)h, _O_BINARY);
> FILE* f = _fdopen(fd, "rb");
> fstream mycin(f);
> // you can also construct a filebuf from FILE*
>

Thanks again for your suggestion.
I need an istream object and not the fstream. So I tried

myFile = new std::istream (std::cin.rdbuf(), std::ios::binary);

instead of

myFile = &std::cin;

But it does the same.... gets an EOF before the end of the data.... I would
expect it to create the myFile in binary mode, or am I assuming wrong?



Re: read from an istream object by Tim

Tim
Thu Sep 21 00:30:56 CDT 2006

"Igor Tandetnik" <itandetnik@mvps.org> wrote:
>manimau <manimau@discussions.microsoft.com> wrote:
>> So now my next quextion is how do I
>> read in binary mode from cin instead of in text mode?
>
>I can't think of any portable way to do that.

Since the whole text-mode/binary-mode morass is a Microsoft-only
abomination, portability is hardly a concern...
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: read from an istream object by Alex

Alex
Thu Sep 21 05:47:29 CDT 2006

"Tim Roberts" wrote:
>>> So now my next quextion is how do I
>>> read in binary mode from cin instead of in text mode?
>>
>>I can't think of any portable way to do that.
>
> Since the whole text-mode/binary-mode morass is a
> Microsoft-only
> abomination, portability is hardly a concern...


What? Why do you think so?

<quote>
27.4.2.1.4 Type ios_base::openmode

1 The type openmode is a bitmask type (17.3.2.1.2). It
contains the elements indicated in Table 86:

...
binary perform input and output in binary mode (as opposed
to text mode)
</quote>



Re: read from an istream object by manimau

manimau
Thu Sep 21 10:19:01 CDT 2006

"manimau" wrote:
>
>
> "Igor Tandetnik" wrote:
>
> > manimau <manimau@discussions.microsoft.com> wrote:
> > > So now my next quextion is how do I
> > > read in binary mode from cin instead of in text mode?
> >
> > I can't think of any portable way to do that. If you are happy with a
> > non-portable one, specific to Win32 and VC compiler, you can do the
> > following:
> >
> > HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
> > int fd = _open_osfhandle((intptr_t)h, _O_BINARY);
> > FILE* f = _fdopen(fd, "rb");
> > fstream mycin(f);
> > // you can also construct a filebuf from FILE*
> >
>
> Thanks again for your suggestion.
> I need an istream object and not the fstream. So I tried
>
> myFile = new std::istream (std::cin.rdbuf(), std::ios::binary);
>
> instead of
>
> myFile = &std::cin;
>
> But it does the same.... gets an EOF before the end of the data.... I would
> expect it to create the myFile in binary mode, or am I assuming wrong?
>
>


I cannot use setmode as microsoft has taken out the setmode member function
for filebuf from .NET 2003.

What is the other method I can use to set the mode binary for the cin?