无法读取整个文件

时间:2013-04-05 17:31:58

标签: c++ file iostream fstream bmp

我正在制作一个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++;
    }
}

现在,如果我在256x256的测试文件上运行它,它将打印正常,直到它达到0x36E,它会给出错误/不会更进一步。发生这种情况是因为 completeStr 字符串无法接收bmp文件中的所有数据。为什么无法读取bmp文件中的所有行?

2 个答案:

答案 0 :(得分:6)

您的代码存在许多问题。校长 一个(可能是你的问题的原因)就是你 以文本模式打开文件。从技术上讲,这意味着如果 该文件包含除可打印字符和一些之外的任何内容 特定的控制字符(如'\ t'),你有未定义的 行为。实际上,在Windows下,这意味着序列 0x0D,0x0A将被转换为单个'\n',并且 一个0x1A将被解释为文件的结尾。并不是的 阅读二进制数据时想要的是什么。你应该打开 以二进制模式传输(std::ios_base::binary)。

不是严重错误,但您不应该使用fstream 如果你只是要阅读文件。事实上,使用一个 fstream应该非常罕见:您应该使用ifstreamofstream。同样的事情适用于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>()) );

答案 1 :(得分:2)

std::getline读入一行文字。

它对二进制文件没用。

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