写入和读入图像的标题

时间:2013-12-06 09:58:21

标签: c++ image file-io binary

我有一张图片,即 warren.jpg 。我写了下面的代码,将我的名字写入其头部渲染图像查看器程序无法打开文件。然后我编写了下一个代码来读取除了我的名字之外的所有内容并将其写入另一个文件。以下是两个程序: -

Code 1:

int main () {

  std::ofstream is ("warren.jpg", ios::binary|ios::out);

  if (is) {
    // get length of file:
    is.seekp(0,is.beg);
    char *wr= "avinash";
    is.write(wr, 7);
    is.close();
  }

  return 0;
}

Code 2:
int main () {

    std::ifstream is ("warren.jpg", ios::binary|ios::in);

  if (is) {
    // get length of file:
    is.seekg (0, is.end);
    int length = is.tellg();
    is.seekg (0, is.beg);

    char *name= new char[7];
    char * buffer = new char[length-7]();
    is.read (name,7);
    is.read (buffer,length-7);
    cout<<name[0]<<name[1]<<name[6]<<endl; //'avh' is printed

    std::ofstream os;
    os.open("warrennew.jpg", ios::binary|ios::out);
    if(os)
    {
        os.write(buffer, length-7);
        cout<<"ok"<<endl;   /this was printed in the output
    }
    delete name;
    delete buffer;
  return 0;
}
}

我的问题是创建的文件( warrennew.jpg )是0字节。有人可以帮忙吗?

0 个答案:

没有答案