0

我正在尝试使用以下示例:

https://stackoverflow.com/a/6832677/1816083 但我有:

invalid conversion from `unsigned char*' to `char*'
initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::read(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]' 
invalid conversion from `void*' to `size_t'

排队:

size_t bytes_read = myfile.read((unsigned char *) buffer, BUFFER_SIZE);
4

2 回答 2

3

首先,read()需要 achar*而不是unsigned char*. 其次,它不返回读取的字符数。

相反,请尝试:

myfile.read((char*)buffer, BUFFER_SIZE);
std::streamsize bytes_read = myfile.gcount();
于 2013-03-07T08:36:51.980 回答
1

恕我直言,编译器的输出已经足够了。它告诉您,您正在尝试赋予unsigned char*功能,等待char*。顺便说一句,甚至还有一个函数名

std::basic_istream<_CharT, _Traits>::read(_CharT*, std::streamsize)
[with _CharT = char ...

如果您需要unsigned chars buffer[ ... ],请将其转换为char*

unsigned char buffer[ BUFFER_SIZE ];
ifstream myfile("myfile.bin", ios::binary);
if (myfile)
{
    myfile.read((char*) buffer, BUFFER_SIZE);
    //          ^^^^^^^
    size_t bytes_read = myfile.gcount();
}
于 2013-03-07T08:42:55.143 回答