By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,494 Members | 1,736 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,494 IT Pros & Developers. It's quick & easy.

iostream - BYTE array

P: n/a
how to use <iostreamto read a file to a const BYTE array?

const BYTE *pbBinary
Nov 19 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Nov 19, 9:13*am, Joy Maitland <iiu...@yahoo.comwrote:
how to use <iostreamto read a file to a const BYTE array?

const BYTE *pbBinary
You don't normally read a file into a pointer. You read a file into an
array and point that pointer to that array:

#include <iostream>
#include <vector>
#include <iterator>

int main()
{
typedef unsigned char BYTE;
// read from std::cin
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(std::cin))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read

BYTE const* pbBinary = &bytes[0];
}

--
Max
Nov 19 '08 #2

P: n/a
why when i change the code from std::cin to read from file
c:/test2.xml I got error

=======================
typedef unsigned char BYTE;
std::ofstream file1("c:/test2.xml");

// read from std::cin
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(file1))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read

BYTE const* pbBinary = &bytes[0];

===========================

1>d:\snd\remote\remotedemo\app\virtualawear\main.c pp(31) : error
C2440: '<function-style-cast>' : cannot convert from 'std::ofstream'
to 'std::istreambuf_iterator<_Elem,_Traits>'
1 with
1 [
1 _Elem=char,
1 _Traits=std::char_traits<char>
1 ]
1 No constructor could take the source type, or constructor
overload resolution was ambiguous

============================


On Wed, 19 Nov 2008 01:22:44 -0800 (PST), Maxim Yegorushkin
<ma***************@gmail.comwrote:
>On Nov 19, 9:13?am, Joy Maitland <iiu...@yahoo.comwrote:
>how to use <iostreamto read a file to a const BYTE array?

const BYTE *pbBinary

You don't normally read a file into a pointer. You read a file into an
array and point that pointer to that array:

#include <iostream>
#include <vector>
#include <iterator>

int main()
{
typedef unsigned char BYTE;
// read from std::cin
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(std::cin))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read

BYTE const* pbBinary = &bytes[0];
}
Nov 19 '08 #3

P: n/a
On Nov 19, 9:36*am, Joy Maitland <iiu...@yahoo.comwrote:
why when i change the code from std::cin to read from file
c:/test2.xml *I got error

=======================
typedef unsigned char BYTE;
* * * * * * * * std::ofstream file1("c:/test2.xml");
Becuase you are using an output stream, rather than an input. Change
ofstream to ifstream.

--
Max
Nov 19 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.