C++与java的java .Io.FielnPixStudio? FileInputStream fi = new FileInputStream(f); byte[] b = new byte[188]; int i = 0; while ((i = fi.read(b)) > -1)// This is the line that raises my question. { // Code Block }

C++与java的java .Io.FielnPixStudio? FileInputStream fi = new FileInputStream(f); byte[] b = new byte[188]; int i = 0; while ((i = fi.read(b)) > -1)// This is the line that raises my question. { // Code Block },java,c++,inputstream,Java,C++,Inputstream,我试图运行以下代码行,但结果是一个错误 ifstream InputStream; unsigned char *byte = new unsigned char[188]; while(InputStream.get(byte) > -1) { // Code Block } 您可以使用和逐个读取单个字符,或者使用提取运算符读取输入流中以纯文本形式显示的任何给定类型,或者读取连续的字节数 注意,与java相反,C++读取返回流。如果想知道读取的字节数,必须使用,或者使用 因

我试图运行以下代码行,但结果是一个错误

 ifstream InputStream;
 unsigned char *byte = new unsigned char[188];
 while(InputStream.get(byte) > -1)
 {
 // Code Block
 }
您可以使用和逐个读取单个字符,或者使用提取运算符读取输入流中以纯文本形式显示的任何给定类型,或者读取连续的字节数

注意,与java相反,C++读取返回流。如果想知道读取的字节数,必须使用,或者使用

因此,可能的解决办法是:

ifstream ifs (f);  // assuming f is a filename
char b[188]; 
int i = 0;
while (ifs.read(b, sizeof(b))) // loop until there's nothing left to read
{
   i = ifs.gcount();   // number of bytes read
   // Code Block
}
您可以使用和逐个读取单个字符,或者使用提取运算符读取输入流中以纯文本形式显示的任何给定类型,或者读取连续的字节数

注意,与java相反,C++读取返回流。如果想知道读取的字节数,必须使用,或者使用

因此,可能的解决办法是:

ifstream ifs (f);  // assuming f is a filename
char b[188]; 
int i = 0;
while (ifs.read(b, sizeof(b))) // loop until there's nothing left to read
{
   i = ifs.gcount();   // number of bytes read
   // Code Block
}

... 有一个从std::istreamjava.io.FileInputStream.read继承的方法返回int,ifstream的read没有。std::istream有readsome,它返回读取字节数td::ifstream::read返回ifstream对象,该对象可以在上下文中转换为bool并进行测试。喜欢whilefile.readbyte{…}。当它失败时,您可以使用来检查失败前读取的字符数。我已发布了我尝试使用的代码行,这会导致错误。。。有一个从std::istreamjava.io.FileInputStream.read继承的方法返回int,ifstream的read没有。std::istream有readsome,它返回读取字节数td::ifstream::read返回ifstream对象,该对象可以在上下文中转换为bool并进行测试。喜欢whilefile.readbyte{…}。当它失败时,您可以使用检查失败前读取的字符数。我已发布了我尝试使用的代码行,并导致错误。我使用get:error:invalid conversion从“unsigned char*”到“std::basic_istream::char_type{aka char}”[-fpermissive]@LucasC.alvesbitncourt抱歉,我以为你想读个人的角色。请看我的编辑。看来这解决了我的问题。非常感谢。我使用get:error:invalid-conversion从“unsigned char*”转换为“std::basic_istream::char_type{aka char}”[-fppermissive]@LucasC.alvesbitencourt收到此错误。对不起,我以为您想读取单个字符。请看我的编辑。看来这解决了我的问题。非常感谢你。