1

嘿,只是尝试在代码中编译图像并再次将其输出为 jpg 但 fwrite 返回 0 并且 img.jpg 保持为空:(

#include <stdio.h>
#include <string>
#include <iostream>

size_t count = 76830;//=length of data

const uint8_t data[] = {0xff,0xd8,0xff,0xe0,0x0,0x10,0x4a,0x46,0x49,0x46.....
0x0,0x7f,0xff,0xd9};

using namespace std;
//use this to saveToFile...
void saveToFile(const char * filename) {
  FILE * file = fopen(filename, "rb");
  if (file == NULL) {
    std::cout << "error opening file" << std::endl;
    return;
  }


  cout << endl << fwrite(data, sizeof(uint8_t), sizeof(data), file); //this line returns 0!!!
  fclose(file);
}

int main () {
  saveToFile("img.jpg");
}
4

1 回答 1

3
fopen(filename, "rb");

这将打开文件进行读取,而不是写入。你要

fopen(filename, "wb");
//               ^
于 2013-08-27T14:57:32.020 回答