Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法读取整个文件 我正在制作一个C++程序,能够打开.bmp图像,然后把它放在一个2D数组中。现在我有这样的代码: #include <iostream> #include <fstream> #include <sstream> #include <string> #include "Image.h" using namespace std; struct colour{ int red; int green; int blue; }; Image::Image(string location){ fstream stream; string tempStr; stringstream strstr; stream.open(location); string completeStr; while(!stream.eof()){ getline(stream, tempStr); completeStr.append(tempStr); } cout << endl << completeStr; Image::length = completeStr[0x13]*256 + completeStr[0x12]; Image::width = completeStr[0x17]*256 + completeStr[0x16]; cout << Image::length; cout << Image::width; cout << completeStr.length(); int hexInt; int x = 0x36; while(x < completeStr.length()){ strstr << noskipws << completeStr[x]; cout << x << ": "; hexInt = strstr.get(); cout << hex << hexInt << " "; if((x + 1)%3 == 0){ cout << endl; } x++; } } #包括 #包括 #包括 #包括 #包括“Image.h” 使用名称空间std; 结构色{ 红色; 绿色; 蓝色; }; Image::Image(字符串位置){ 流; 字符串tempStr; stringstream strstr; 溪流开放(位置); 字符串完成器; 而(!stream.eof()){ getline(stream,tempStr); completeStr.append(tempStr); } cout_C++_File_Iostream_Fstream_Bmp - Fatal编程技术网

无法读取整个文件 我正在制作一个C++程序,能够打开.bmp图像,然后把它放在一个2D数组中。现在我有这样的代码: #include <iostream> #include <fstream> #include <sstream> #include <string> #include "Image.h" using namespace std; struct colour{ int red; int green; int blue; }; Image::Image(string location){ fstream stream; string tempStr; stringstream strstr; stream.open(location); string completeStr; while(!stream.eof()){ getline(stream, tempStr); completeStr.append(tempStr); } cout << endl << completeStr; Image::length = completeStr[0x13]*256 + completeStr[0x12]; Image::width = completeStr[0x17]*256 + completeStr[0x16]; cout << Image::length; cout << Image::width; cout << completeStr.length(); int hexInt; int x = 0x36; while(x < completeStr.length()){ strstr << noskipws << completeStr[x]; cout << x << ": "; hexInt = strstr.get(); cout << hex << hexInt << " "; if((x + 1)%3 == 0){ cout << endl; } x++; } } #包括 #包括 #包括 #包括 #包括“Image.h” 使用名称空间std; 结构色{ 红色; 绿色; 蓝色; }; Image::Image(字符串位置){ 流; 字符串tempStr; stringstream strstr; 溪流开放(位置); 字符串完成器; 而(!stream.eof()){ getline(stream,tempStr); completeStr.append(tempStr); } cout

无法读取整个文件 我正在制作一个C++程序,能够打开.bmp图像,然后把它放在一个2D数组中。现在我有这样的代码: #include <iostream> #include <fstream> #include <sstream> #include <string> #include "Image.h" using namespace std; struct colour{ int red; int green; int blue; }; Image::Image(string location){ fstream stream; string tempStr; stringstream strstr; stream.open(location); string completeStr; while(!stream.eof()){ getline(stream, tempStr); completeStr.append(tempStr); } cout << endl << completeStr; Image::length = completeStr[0x13]*256 + completeStr[0x12]; Image::width = completeStr[0x17]*256 + completeStr[0x16]; cout << Image::length; cout << Image::width; cout << completeStr.length(); int hexInt; int x = 0x36; while(x < completeStr.length()){ strstr << noskipws << completeStr[x]; cout << x << ": "; hexInt = strstr.get(); cout << hex << hexInt << " "; if((x + 1)%3 == 0){ cout << endl; } x++; } } #包括 #包括 #包括 #包括 #包括“Image.h” 使用名称空间std; 结构色{ 红色; 绿色; 蓝色; }; Image::Image(字符串位置){ 流; 字符串tempStr; stringstream strstr; 溪流开放(位置); 字符串完成器; 而(!stream.eof()){ getline(stream,tempStr); completeStr.append(tempStr); } cout,c++,file,iostream,fstream,bmp,C++,File,Iostream,Fstream,Bmp,std::getline读取一行文本 它对于二进制文件没有用处 以二进制模式打开文件,并使用未格式化的输入操作(如)。您的代码存在许多问题。主体 一个(可能是你问题的原因)是你 以文本模式打开文件。从技术上讲,这意味着如果 该文件包含除可打印字符和少量字符以外的任何内容 特定的控制字符(如“\t”),您有未定义的 实际上,在Windows下,这意味着 对于0x0D,0x0A将转换为单个'\n',并且 0x1A将被解释为文件的结尾。实际上并非如此 读取二进制数据时需要什么。您应该打开 二进制模式下

std::getline
读取一行文本

它对于二进制文件没有用处


以二进制模式打开文件,并使用未格式化的输入操作(如)。

您的代码存在许多问题。主体 一个(可能是你问题的原因)是你 以文本模式打开文件。从技术上讲,这意味着如果 该文件包含除可打印字符和少量字符以外的任何内容 特定的控制字符(如“\t”),您有未定义的 实际上,在Windows下,这意味着 对于0x0D,0x0A将转换为单个
'\n'
,并且 0x1A将被解释为文件的结尾。实际上并非如此 读取二进制数据时需要什么。您应该打开 二进制模式下的流(
std::ios\u base::binary

这不是一个严重错误,但您不应该真正使用
fstream
如果你只想读取文件,事实上,使用
fstream
应该非常罕见:您应该使用
ifstream
或者流的
stringstream的情况也一样(但是
在读取二进制文件时,我看不到
stringstream
的任何角色 文件)

另外(这是一个真正的错误),您正在使用
getline
不检查是否成功。通常 阅读台词的习惯用法是:

while ( std::getline( source, ling ) ) ...
但是像
stringstream
,您不想在上使用
getline
二进制流;它将删除所有的
'\n'
(具有 已从CRLF映射)

如果希望所有数据都存储在内存中,最简单的解决方案是 比如:

std::ifstream source( location.c_str(), std::ios_base::binary );
if ( !source.is_open() ) {
    //  error handling...
}
std::vector<char> image( (std::istreambuf_iterator<char>( source ) ),
                         (std::istreambuf_iterator<char>()) );
std::ifstream源(location.c_str(),std::ios_base::binary);
如果(!source.is_open()){
//错误处理。。。
}
std::vector image((std::istreambuf_迭代器(源)),
(std::istreambuf_迭代器());

while(!eof())是。如果你是这样的话,我不会感到惊讶。你可以考虑把位图当作二进制文件来读取,而不是一个文件的集合。谢谢。我是一个java程序员,试图转移到C++。从二进制文件中提取文件大小,然后做一个这么大的数组不是更快吗?我知道向量比数组慢,因为它需要调整大小。@user2250025可能有点小。不过,我觉得他后来在做图像处理,我不认为差别有多大。更不用说没有可移植的方法来确定文件大小,如果你开始接受不可移植的cons卡车,
mmap
速度明显更快。