while( !feof(card))
{
FILE *f1=fopen(gp[i],"a"); //Opens a new file to store the bytes in
fread(buffer1, sizeof(BYTE), 512, card); //Reads a set of bytes from the card
if (buffer1[0] == 0xff && buffer1[1] == 0xd8 && buffer1[2] == 0xff && buffer1[3]/0x10==0x0e) //Checks whether the first 4 bytes match the start of a jpg
{
fwrite(buffer1, sizeof(BYTE), 512, f1); //Writes the bytes on to the opened file
fread(buffer1, sizeof(BYTE), 512, card); //Reads the next set of bytes from the card
while (buffer1[0] != 0xff && buffer1[1] != 0xd8 && buffer1[2] != 0xff && buffer1[3]/0x10!=0x0e) //Repeats the below process till we find the start of a new jpg
{
fwrite(buffer1, sizeof(BYTE), 512, f1); //Writes the bytes on to the opened file
fread(buffer1, sizeof(BYTE), 512, card); //Reads the next set of bytes from the card
}
fclose(f1);
i++;
}
Asked
Active
Viewed 69 times
0

Jabberwocky
- 48,281
- 17
- 65
- 115
-
3You should read: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – UnholySheep May 01 '20 at 10:40
-
Does this answer your question? [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Enis Arik May 01 '20 at 10:59
-
I changed it to while j<50 and modified it to read only 50 images. It's still not working – Dineth Hettiarachchi May 01 '20 at 11:22
-
How is `buffer1` defined and initialised? – alk May 01 '20 at 12:40
-
Also, does the real code lack all error checking? – alk May 01 '20 at 12:41
-
At least one problem is the second while condition. See [this answer](https://cs50.stackexchange.com/questions/28538/pset4-recover-recovers-49-images-but-not-last-image-and-also-doesnt-pass-chec/28542#28542) for full explanation. – DinoCoderSaurus May 01 '20 at 13:59
-
buffer1 is defined as an array of 512 BYTEs – Dineth Hettiarachchi May 02 '20 at 05:19
-
Sorry there's no error checking, I'm still a beginner – Dineth Hettiarachchi May 02 '20 at 05:19
-
Being a beginner is no excuse for not doing error checking. Error checking is not something annoying but it will help you detect problems in your code early. – Jabberwocky May 02 '20 at 07:31
-
I don't know how to do error checking. If you could tell me a good place to learn how to do error checking I will try it – Dineth Hettiarachchi May 02 '20 at 09:18
-
@DinethHettiarachchi read the documentation of `fopen` and especially about what happens if the file you try to open cannot be opened, for example because it does not exist. – Jabberwocky May 02 '20 at 09:25
-
@DinoCoderSaurus I altered the while condition as pointed out but now I'm getting only 1 jpg and that's not being opened either – Dineth Hettiarachchi May 02 '20 at 09:41
-
@Jabberwocky fopen is supposed to give me a pointer to a file right? – Dineth Hettiarachchi May 02 '20 at 09:50
-
@DinethHettiarachchi exactly, but what does it "give to you" if the file __does not exist__? It's written in the documentation. – Jabberwocky May 02 '20 at 09:58
-
@Jabberwocky I'm supposed to get a null pointer and apparently a global variable errno is set to indicate the error. I don't understand what's happening there. I feel like I'm missing something – Dineth Hettiarachchi May 02 '20 at 10:46
-
@DinethHettiarachchi roughly like this: if `fopen` returns NULL, then print an error message and abort, otherwise proceed. This is most basic programming knowledge. – Jabberwocky May 02 '20 at 10:59
-
@Jabberwocky my program compiles though and doesn't print out error messages – Dineth Hettiarachchi May 02 '20 at 11:36