2011-06-18 229 views
-2

我需要从在C jpg文件读取的字节++这样写代码:读取二进制字节

ifstream in("1.jpg"ios::binary); 
    while(!in.eof()) 
{ 
char ch = in.get(); 
} 

你知道一个jpg文件包含256区别的字符,我们可以将它保存的重复在一个arr.but问题是,我写的这段代码以unicode的形式读取字符,所以它包含9256区别char.how我可以从1.jpg读取它不是unicode吗?

+1

.jpeg包含字节,而不是字符。将in.get()的返回值转换为(unsigned char)。 –

+0

thnaks为您的提示 –

回答

2

get函数从文件中读取未格式化的数据,它只是将其读取的字符转换为int。你是否看到从文件读取的数据与文件中的实际数据不同?如果你在代码的其他地方可能存在问题,那么你应该提供更多的东西。

或者,您可以使用read来读取未格式化数据的块。

int main() 
{ 
    std::ifstream in("1.jpg", std::ios::binary); 

    char buffer[1024]; 

    while (in) 
    { 
     in.read(buffer, sizeof(buffer)); 

     if (in.gcount() > 0) 
     { 
      // read in.gcount() chars from the file 
      // process them here. 
     } 
    } 
}