问题描述
||
我需要从c ++中的jpg文件中读取字节,因此请编写以下代码:
ifstream in(\"1.jpg\"ios::binary);
while(!in.eof())
{
char ch = in.get();
}
您知道一个jpg文件由256个不同的字符组成,我们可以将其保存在aa arr。中,但问题是我编写的这段代码以unicode的形式读取字符,因此它由9256个不同的char.how组成我可以从1.jpg读到不是Unicode吗?
解决方法
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.
}
}
}