#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//Define BYTE
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{//Check if command line is correct//
if (argc != 2)
{
printf("invalid entry");
return 1;
}//read memory card to file
FILE *file = fopen(argv[1], "r");
//check if it is valid//
if (file == NULL)
{
printf("file is empty");
return 1;
}
//declarations
BYTE buffer [512];
//string character for file name
char fname [8];
//initialize file number to begin from 0
int fnumber = 0;
//create new file
FILE *nf = NULL;
//check if not end of file
while (!feof(file))
{// copy bytes to buffer
fread(buffer, sizeof(buffer), 1, file);
//check if beginning of file
if (buffer [0] == 0xff &&
buffer [1] == 0xd8 &&
buffer [2] == 0xff &&
(buffer [3] & 0xf0) == 0xe0)
{//check if first file
if (fnumber == 0)
{//write file name
sprintf(fname, "%03i.jpg", fnumber);
//increment file number
fnumber++;
//open new file *nf
nf = fopen(fname, "w");
//write to new file
fwrite(nf, sizeof(buffer), 1, buffer);
}//if not first file
else
{//close opened file
fclose(nf);
sprintf(fname, "%03i.jpg", fnumber);
fnumber++;
//open new file
nf = fopen(fname, "w");
fwrite(nf, sizeof(buffer), 1, buffer);
}
}//if not beginning of file
else
{//check if nf is still null
if (nf != NULL)
{//continue writing to opened file
fwrite(nf, sizeof(buffer), 1, buffer);
}
}
}
fclose(nf);
fclose(file);
return 0;
}
-
This code doesn't compile. Are you sure this is the correct version of the code that you're running? Please edit the question and post the actual working code that you are using. – Cliff B Jun 15 '20 at 19:32
-
Yes you are right, the code doesn't compile. I've been using check50 without running make. I have another question now that I've corrected it – Olhuwhorlhey Jun 17 '20 at 18:05
Add a comment
|
I was writing from *nf to buffer instead of the opposite. Before
fwrite(nf, sizeof(buffer), 1, buffer)
Now
fwrite(buffer, sizeof(buffer), 1, nf)
Array itself is a pointer.
You don't need to use *(pointer)
to point to an array.
-
Thank you for that, new problem though. buffer is not compatible with file *nf; how & why? – Olhuwhorlhey Jun 17 '20 at 20:28
-
You can only use
sizeof()
with any of the data types that C offers, likesizeof(int)
sizeof(char)
etc. – Shahroz Ali Jun 18 '20 at 8:43 -
Because buffer itself isn't a data type but it is the name of an array, So you should know about the size of each byte in a JPEG, Byte contains 512 blocks in which size of each block is???? – Shahroz Ali Jun 18 '20 at 8:45
-
If you found the answer be sure to tick the answer if I helped you.Thanks – Shahroz Ali Jun 18 '20 at 8:46