fwrite对大数据失败了

时间:2013-08-27 14:55:27

标签: c++ fwrite

嘿只是尝试在代码中编译图像并再次将其作为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");
}

1 个答案:

答案 0 :(得分:3)

fopen(filename, "rb");

这将打开文件进行阅读,而不是写作。你想要

fopen(filename, "wb");
//               ^