###Binary mode###
Binary mode
You should use binary mode to open your file instead of text mode. So change:
FILE* inptr = fopen(infile, "r");
to
FILE* inptr = fopen(infile, "rb");
Otherwise, your CR/LF characters may get converted when you don't want them to be.
###Buffer overrun###
Buffer overrun
Your filename buffer is too short:
char filename[7]; char * name() { if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum); // ...
Here, your filenames will be something like "000.jpg"
which require 7 characters plus a terminating null character, or 8 characters in total. Also, if you generate 100 or more files, you will need even more characters.
###Pad string with leading zeros###
Pad string with leading zeros
There is a better way to generate filenames padded with leading zeros. Instead of:
if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum);
you can do this:
sprintf(filename, "%03d.jpg", filenum);
###Binary mode###
You should use binary mode to open your file instead of text mode. So change:
FILE* inptr = fopen(infile, "r");
to
FILE* inptr = fopen(infile, "rb");
Otherwise, your CR/LF characters may get converted when you don't want them to be.
###Buffer overrun###
Your filename buffer is too short:
char filename[7]; char * name() { if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum); // ...
Here, your filenames will be something like "000.jpg"
which require 7 characters plus a terminating null character, or 8 characters in total. Also, if you generate 100 or more files, you will need even more characters.
###Pad string with leading zeros###
There is a better way to generate filenames padded with leading zeros. Instead of:
if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum);
you can do this:
sprintf(filename, "%03d.jpg", filenum);
Binary mode
You should use binary mode to open your file instead of text mode. So change:
FILE* inptr = fopen(infile, "r");
to
FILE* inptr = fopen(infile, "rb");
Otherwise, your CR/LF characters may get converted when you don't want them to be.
Buffer overrun
Your filename buffer is too short:
char filename[7]; char * name() { if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum); // ...
Here, your filenames will be something like "000.jpg"
which require 7 characters plus a terminating null character, or 8 characters in total. Also, if you generate 100 or more files, you will need even more characters.
Pad string with leading zeros
There is a better way to generate filenames padded with leading zeros. Instead of:
if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum);
you can do this:
sprintf(filename, "%03d.jpg", filenum);
###Binary mode###
You should use binary mode to open your file instead of text mode. So change:
FILE* inptr = fopen(infile, "r");
to
FILE* inptr = fopen(infile, "rb");
Otherwise, your CR/LF characters may get converted when you don't want them to be.
###Buffer overrun###
Your filename buffer is too short:
char filename[7]; char * name() { if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum); // ...
Here, your filenames will be something like "000.jpg"
which require 7 characters plus a terminating null character, or 8 characters in total. Also, if you generate 100 or more files, you will need even more characters.
###Pad string with leading zeros###
There is a better way to generate filenames padded with leading zeros. Instead of:
if (filenum < 10) sprintf(filename, "00%d.jpg", filenum); else sprintf(filename, "0%d.jpg", filenum);
you can do this:
sprintf(filename, "%03d.jpg", filenum);