Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

Reading binary file into char array in c++

std::streampos size;
char * memblock;

std::ifstream input ("A.JPG", std::ios::in|std::ios::binary|std::ios::ate);

if (input.is_open())
{
    size = input.tellg();
    memblock = new char [size];
    input.seekg (0, std::ios::beg);
    input.read (memblock, size);
    input.close();

    std::cout << "[INPUT]the entire file content is in memory " << sizeof(memblock) << " 
";

}
delete[] memblock;

I would like to use the ifstream to read A.JPG (28KB) and save it into the array memblock. But why do the size of the memblock is 4 instead of 28403 while the variable size is equal to 28403?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because memblock is a pointer, so the sizeof operator evaluates to the size of the pointer variable, which is 4 bytes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...