我正在尝试将一个png文件作为char数组读取并通过套接字发送,但是该文件并没有真正加载...这是代码:
ifstream file;
char* image;
file.open("C:\\image.png", ios::binary);
if (file.is_open())
{
file.seekg(0, ios::end);
unsigned int length = file.tellg();
file.seekg(0, ios::beg);
image = new char[length];
file.read(image, length);
file.close();
unsigned int arr_len = 0;
while (image[arr_len] != 0)
{
arr_len++;
}
cout << "file size: " << length << ", array size: " << arr_len << endl;
}
else
{
image = (char*)"";
cout << "Failed to open image." << endl;
}
它打印:文件大小:30958,数组大小:8
知道发生了什么吗?
编辑:更新了该愚蠢的指针错误,但现在它打印出的大小是8?谢谢帮忙