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) << " \n";
}
delete[] memblock;
我想使用ifstream读取A.JPG(28KB)并将其保存到数组memblock中。但是为什么memblock的大小是4而不是28403而变量大小等于28403?
谢谢。
答案 0 :(得分:0)
因为memblock
是一个指针,所以sizeof
运算符的计算结果是指针变量的大小,即4个字节。
答案 1 :(得分:0)
谢谢大家,最后我用了向量。 因为似乎很难显示我想要的结果(实际char数组的长度)
std::vector <char> memblock(0);
if (input.is_open())
{
size = input.tellg();
//memblock = new char [size];
memblock.resize(size);
input.seekg (0, std::ios::beg);
input.read (&memblock[0], size);
input.close();
//std::cout << "[INPUT]the entire file content is in memory " << ((char *)(&memblock+1) - (char *)memblock) / (sizeof(memblock[0])) << " \n";
std::cout << "[INPUT]the entire file content is in memory " << memblock.size() << " \n";