user2722085
user2722085

Reputation: 99

fwrite fails on big data

Hey just try to compile an image in code and put it out as jpg again but fwrite returns 0 and img.jpg keeps empty :(

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

Upvotes: 1

Views: 220

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254421

fopen(filename, "rb");

This opens the file for reading, not writing. You want

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

Upvotes: 3

Related Questions