Why does calling istream::tellg() affect the behavior of my program?

岁酱吖の 提交于 2019-12-23 12:29:05

问题


I am trying to convert a 24 bit bitmap image into grayscale.

#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;
}

When I remove the cout tellg statement the loop runs only two times!

I don't understand what difference removing a cout statement make?

Result: Only one pixel changes to grayscale.

I found a simpler version of my problem here

Reading and writing to files simultaneously?

But didnt find a solution...


回答1:


When reading and writing a std::fstream you need a seek when switching between reading and writing. The reason for this is that file streams share a common input and output position. To also support efficient buffering it is necessary to inform the respective other buffer about the current position. This is part of what the seek does. tellg() does a seek to the current position.

Note, that it is very inefficient to switch between reading and writing, especially when the implementation is well optimized. You'd be much better off either writing a different file or updating values in reasonable sized groups.



来源:https://stackoverflow.com/questions/12546036/why-does-calling-istreamtellg-affect-the-behavior-of-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!