user1926691
user1926691

Reputation: 173

Fwrite output file 0kb

I tried writing to a file using fwrite as shown below but my output file is always 0Kb. It works fine with txt files. Please advise me on what I should do . I am still a newbie at this .

Thank you.

#include "stdafx.h"
#include <string>
#include <fstream>
#include <Windows.h>
using namespace std;
unsigned char* buffer;
int ch,size;
int count =0;


FILE *FinalPkt;
FILE *PdfInput;

int _tmain(int argc, _TCHAR* argv[])
{
    PdfInput=fopen("C:\\test.pptx","rb");
    fseek(PdfInput,0,SEEK_END);
    size=ftell(PdfInput);
    fseek(PdfInput,0,SEEK_SET);
    buffer=(unsigned char *)malloc((unsigned int)_MAX_PATH);
    fread(&buffer,sizeof(unsigned int),size,PdfInput);

    if (PdfInput==NULL)
    {
        int i =1;
    }
    FinalPkt = fopen("C:\\test1.pptx","wb");
    fwrite(&buffer,size,1,FinalPkt);
    fclose(FinalPkt);
    if (FinalPkt==NULL)
    {

    }
    return 0;
}  

Upvotes: 1

Views: 321

Answers (1)

unwind
unwind

Reputation: 399889

This:

fwrite(&buffer, size, 1, FinalPkt);

passes the address of buffer, which is already a pointer, to fwrite(). This is wrong.

It should be:

fwrite(buffer, size, 1, FinalPkt);

Also note that fwrite() can fail, you should check its return value.

Upvotes: 2

Related Questions