0
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc!=2)
    {
        fprintf(stderr, "./recover nameofimagefile\n");
        return 1;
    }

    char *rawfile = argv[1];
    FILE *inptr = fopen(rawfile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", rawfile);
        return 2;
    }

    typedef unsigned char BYTE;
    BYTE buffer[512];
    int p=0;
    char *newfile = malloc(sizeof(buffer)*p);
    int filecounter=0;
    FILE *outptr = fopen(newfile, "w");
    if (outptr == NULL)
    {
        fprintf(stderr, "Could not open for writing.\n");
        return 3;
    }

I dont understand why it keeps returning 3 to me, how do i stop this problem

1
int p=0;
char *newfile = malloc(sizeof(buffer)*p);

You actually have zero bytes reserved with malloc

1

Also, newfile needs to contain the name of the file to be opened. It never has the filename assigned to it.

  • Isn't newfile the file I created to store jpeg images? Why does it need additional assignment? – Shane Jul 30 '17 at 3:13
  • No, newfile is a string that holds the name of the file to be opened. It is not the actual file pointer and it has no direct connection to the actual file other than holding the file's name. That file name is taken from newfile and given to the filepointer outptr by the fopen() call. – Cliff B Jul 30 '17 at 3:23
  • So what i can do is simply define it as an array right? Like char newfile[]? Because i will name it later on so right now i want to simply create a storage for it – Shane Jul 30 '17 at 9:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .