小编Dip*_*shu的帖子

为什么调用istream :: tellg()会影响我的程序的行为?

我试图将24位位图图像转换为灰度图像.

#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class pixel{
            public:
                   unsigned char b;
                   unsigned char g;
                   unsigned char r;
            void display()
            {
                 cout<<r<<" "<<g<<" "<<b<<" ";
                 }
      }p1;
using namespace std;
int main(){
    unsigned char avg;
    fstream file("image.bmp",ios::binary|ios::in|ios::out);

    int start;
    file.seekg(10);
    file.read((char*)&start,4);


    file.seekg(start);
    int i=0;
   while(!file.eof()){
                      cout<<file.tellg();//Remove this and the program doesn't work!
                     file.read((char*)&p1,3);
                     avg=(p1.b+p1.g+p1.r)/3;
                     p1.b=avg;
                     p1.g=avg;
                     p1.r=avg;
                     file.seekg(-3,ios::cur);
                     file.write((char*)&p1,3);
                       }
    file.close();
    getch();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我删除cout tellg语句时,循环只运行两次!

我不明白删除cout语句有什么区别?

结果:只有一个像素变为灰度.

我在这里找到了一个更简单的问题版本

同时读写文件?

但没有找到解决方案......

c++ fstream bmp file-handling grayscale

4
推荐指数
1
解决办法
590
查看次数

标签 统计

bmp ×1

c++ ×1

file-handling ×1

fstream ×1

grayscale ×1