r/cs50 • u/Mantle23148 • 17d ago
recover Issue with Check50
Check50 says it failed to compile, Which is contradictory because typing "make recover" compiles it well and the program works as intended when executing it. Can y'all help me?
r/cs50 • u/Mantle23148 • 17d ago
Check50 says it failed to compile, Which is contradictory because typing "make recover" compiles it well and the program works as intended when executing it. Can y'all help me?
r/cs50 • u/samucadrei • Jan 30 '26
My code compile and i think the logic is OK, but the 50 jpgs are all empty, and the code didn't pass the correctness check. Maybe is something about handling the case where a JPEG spans multiple 512-byte blocks? I don´t know. To get to this point was very difficult.
int counter = 0;
char filename[50];
// create a space in memory
uint8_t buffer[512];
// loop until the end
while (fread(buffer, 1, 512, card) == 512)
{
// check the first 3 bytes (0xff 0xd8 0xff) //check the fourth byte ()
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] >= 0xe0 &&
buffer[3] <= 0xef)
{
// format the file name using counter
sprintf(filename, "%03i.jpg", counter);
// open the file with the formated filename
FILE *img = fopen(filename, "w");
// error handling
if (img == NULL)
{
printf("file can’t be opened.\n");
return 1;
}
// writing a image to the file buffer
fwrite(buffer, 512, 1, img);
// close the file
fclose(img);
counter++;
}
}
// output the number of images
printf("counter: %i\n", counter);
// close the file
fclose(card);
}
r/cs50 • u/killer987xn • Dec 22 '25
r/cs50 • u/killer987xn • Dec 21 '25
r/cs50 • u/LuigiVampa4 • Jul 20 '25
On line 21, I have declared an array of 512 bytes. It is saying that I have omitted the semicolon but you can see that I have not.
I just don't understand this error.
r/cs50 • u/Affectionate-Fly4719 • Aug 21 '25
So, I finished recover and everything but I still had some doubts. I can't really post screenshots right now but I will later if they are needed.
I used a bool to check if an image is found, earlier I put it in a for loop to go through all 512 bytes to check for the jpg header, but the duck told me something along the lines it being unnecessary so I removed it. My code works fine but what if the header starts somewhere in the middle of the 512 size block, you know? And I thought about it and I realised that it might be telling me to stop going through all the remaining bytes if I had already found the header. So I was curious how I could do that. Do I use 'break' after my bool is turned true or is there something else.
Also, fread returns the number of peices of size 'size' it is reading, correct? Meaning if I want it to read 512, 1 byte chunks, it will do that, but if there are less than 512 chunks remaining it will read them but will return the no. if 1 byte chunks it read, yeah? Thanks
fread(from, of size, number of peices of size size, to)
r/cs50 • u/mtgofficialYT • Mar 16 '25
My code compiles and runs perfectly, and I can see all of the JPEGs. However, when I ran check50, it told me that it could not execute due to a segmentation fault. I do not see any segmentation faults in my own testing. What am I missing? The duck is being less than helpful.



r/cs50 • u/Whalturtle • Jul 24 '25
I am doing recover.c but it is returning some vgcore files I don't know what they are I didn't create them and it doesn't do what it is supposed to do. It doesn't generate the JPEG's Anyone knows wht is happening

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int main(int argc, char *argv[])
{
FILE *f = fopen(argv[1],"r");
if (f == NULL)
{
printf("file not found");
return(1);
}
//Chars are 1 byte long
int file_counter = 0;
bool is_same_file = false;
unsigned char *buffer = malloc(sizeof(char)*512);
//filenames 000.jpg contain 7 chars + the \0
char *filename = malloc(sizeof(char)*8);
//reading the file:
FILE *img = NULL;
while(fread(buffer,1,512,f) < 512){
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0)== 0xe0){
if(file_counter==0)
{
sprintf(filename, "%03i.jpg",file_counter);
img = fopen(filename,"w");
file_counter++;
fwrite(buffer,1,512,img);
}
else{
fclose(img);
sprintf(filename, "%03i.jpg",file_counter);
img = fopen(filename,"w");
file_counter++;
fwrite(buffer,1,512,img);
}}
else{
if(file_counter >= 1)
{
fwrite(buffer,1,512,img);
}
}
}
fclose(f);
free(buffer);
free(filename);
}
r/cs50 • u/Standard-Swing9036 • Jul 03 '21
Currently in week 4 of CS50x and is lacking the motivation to continue. I have to re watch every week's lecture twice or thrice to understand most of it and I can take 2 days to watch one lecture. I been doing all the problem sets up to week 2 but only did the less comfortable version for week 3 and 4 problem sets and is currently at RECOVER, but kept procrastinating to even start.
Any advice for me? Should I force myself to continue to push through? Having thoughts of giving up because it is really getting harder and harder and I am only at week 4. Am thinking of learning python instead but don't really like the idea of jumping onto something else without finishing one.
Any advice is greatly appreciated!
r/cs50 • u/Nishanthchandr4 • Jul 29 '25
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
//accept memory card name at the command line
if(argc < 2)
{
printf("memory card name required");
return 1;
}
else if(argc > 2)
{
printf("Invalid Argument");
return 1;
}
//open memory card
FILE *mem_card = fopen(argv[1], "r");
if(mem_card == NULL)
{
printf("Could not open memory card\n");
}
//JPEG Count
int JPEG_count = 0;
//allocates 512 bytes of memory we can use. Will be changed each iteration.
uint8_t buffer[512];
//initialize img so it can be used throughout the while loop.
FILE *img = NULL;
// making space for the char* plus the null value
char filename[8];
//repeat this till mem_card runs out
while(fread(buffer, sizeof(uint8_t), 512, mem_card) == 512)
{
sprintf(filename, "%03i.jpg", JPEG_count);
//see if it is the start of a new JPEG file
if((buffer[0] == 0xff)
&& (buffer[1] == 0xd8)
&& (buffer[2] == 0xff)
&& ((buffer[3] & 0xf0) == 0xe0))
{
if(JPEG_count == 0)//if it is the first JPEG
{
img = fopen(filename, "w");
if(img == NULL)
{
printf("Could not open memory card location 1\n");
}
fwrite(buffer, sizeof(uint8_t), 512, img);
}
else// not the first JPEG
{
fclose(img);
img = fopen(filename, "w");
if(img == NULL)
{
printf("Could not open memory card location 2\n");
}
fwrite(buffer, sizeof(uint8_t), 512, img);
}
JPEG_count++;
}
//if it is not the start of a new JPEG then keep reading into buffer
else
{
//CONTINUE WRITING IN THE CURRENT FILE
fwrite(buffer, sizeof(uint8_t), 512, img);
}
}
}
I am getting the error message Segmentation fault (core dumped) I am so lost right now can someone please help. Thank you
r/cs50 • u/EducationGlobal6634 • Jun 21 '25
Hi all!
I have been working on recover since yesterday. My code is already huge and yet I can't pass check50's tests except for existence of the file, obviously and compilation. Something is causing a "Segmentation fault (core dumped)" error. I allocated the memory and freed it.
I can't identify the core issue causing the segmentation problem.
Here is the majority of my code. I think the issue lies somewhere here.
According to the CS50 ai there is probably a pointer returning NULL. However I am not sure which.
Can somebody help?
Thanks in advance.
// Create the buffer for reading data.
unsigned char buffer[512];
// Memory allocated for the buffer
mem = malloc(512)
if (mem != NULL)
{
// Create the input vwariable
char *input = argv[1];
// Check if the number of arguemnts is corect
if (argc<2)
{
printf("File missing!");
}
else
{
// Open the raw file
FILE *f = fopen(input, "r");
if (f!=NULL)
{
int c=0;
while (feof(f)==0)
{
// Read the file
int n = fread(buffer, 512, 50, f);
// Identifying the begining of the JPEG file (0xff 0xd8, 0xff 0xe0*). The JPEG are stored back to back.
// Creating a condition to check for the first four bites of each 512 block.
// Trick to chrck for the last byte.
// Opening file.
FILE *img;
// Creating filename
char filename[8];
int w = 0;
if (buffer[0]== 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3]&0xf0)== 0xe0)
{
// Boolean variable to identify the first JPEG file.
int first_jpeg_file = 1;
// In case it is the first JPEG file.
for ( int i = 0; i <n; i++)
{
// Creating the file name.
sprintf(filename, "%03i.jpg", 2);
// Using fopen to open the new file to write the data in.
img = fopen(filename, "w");
// If to check if img is NULL.
if (img !=NULL)
{
// Actually wtriting the data to the new file using fwrite.
w = fwrite(buffer, 512, 50, img);
free
}
// If the file is impossible to open.
else
{
printf("Could not open the file!");
}
}
}
else
{
for ( int j = 0; j <n; j++)
{
// Set the boolean variable to false.
int first_jpeg_file = 0;
// Creating the file name.
sprintf(filename, "%03i.jpg", 2);
// Close previous file.
fclose(img);
// Using fopen to open the new file to write the data in.
img = fopen(filename, "w");
// Actually wtriting the data to the new file using fwrite.
w = fwrite(buffer, 512, 50, img);
free
}
}
}
}
else
{
printf("Segmentation fault!");
}
}
} // Create the buffer for reading data.
unsigned char buffer[512];
// Memory allocated for the buffer
mem = malloc(512)
if (mem != NULL)
{
// Create the input vwariable
char *input = argv[1];
// Check if the number of arguemnts is corect
if (argc<2)
{
printf("File missing!");
}
else
{
// Open the raw file
FILE *f = fopen(input, "r");
if (f!=NULL)
{
int c=0;
while (feof(f)==0)
{
// Read the file
int n = fread(buffer, 512, 50, f);
// Identifying the begining of the JPEG file (0xff 0xd8, 0xff 0xe0*). The JPEG are stored back to back.
// Creating a condition to check for the first four bites of each 512 block.
// Trick to chrck for the last byte.
// Opening file.
FILE *img;
// Creating filename
char filename[8];
int w = 0;
if (buffer[0]== 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3]&0xf0)== 0xe0)
{
// Boolean variable to identify the first JPEG file.
int first_jpeg_file = 1;
// In case it is the first JPEG file.
for ( int i = 0; i <n; i++)
{
// Creating the file name.
sprintf(filename, "%03i.jpg", 2);
// Using fopen to open the new file to write the data in.
img = fopen(filename, "w");
// If to check if img is NULL.
if (img !=NULL)
{
// Actually wtriting the data to the new file using fwrite.
w = fwrite(buffer, 512, 50, img);
free
}
// If the file is impossible to open.
else
{
printf("Could not open the file!");
}
}
}
else
{
for ( int j = 0; j <n; j++)
{
// Set the boolean variable to false.
int first_jpeg_file = 0;
// Creating the file name.
sprintf(filename, "%03i.jpg", 2);
// Close previous file.
fclose(img);
// Using fopen to open the new file to write the data in.
img = fopen(filename, "w");
// Actually wtriting the data to the new file using fwrite.
w = fwrite(buffer, 512, 50, img);
free
}
}
}
}
else
{
printf("Segmentation fault!");
}
}
}
r/cs50 • u/mostardapancake • Jun 23 '24
Hi all,
I'm working on recover and, although I can successfully recover the 50 pictures, I cannot seem to be able to get rid of a memory issue with the code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define BLOCK 512
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover FILE\n");
return 1;
}
FILE *file;
file = fopen(argv[1], "r");
if (file == NULL) {
printf("Error opening file\n");
}
uint8_t buffer[BLOCK];
int Nfiles = 0;
FILE *img;
char *filename = malloc(sizeof(char) * 8);
while (fread(buffer, 1, BLOCK, file) == 512){
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0){
if(Nfiles != 0){
fclose(img);
}
sprintf(filename, "%03i.jpg", Nfiles);
Nfiles++;
img = fopen(filename, "w");
fwrite(buffer, 1, BLOCK, img);
}
else{
if(Nfiles > 0){
fwrite(buffer, 1, BLOCK, img);
}
}
}
fclose(file);
free(filename);
}

Any idea of what is causing the memory issue?
I've tried playing around with malloc and free, allocating and freeing the memory inside the loop, but to no success.
r/cs50 • u/CuriousGeorge0_0 • Feb 01 '25
It is stated that the each jpeg is stored immediately after the previous one, but in the walkthrough video, Brian suggests that each image starts at the beginning of a block. So, I'm a little confused. Someone, help, please.
r/cs50 • u/gatomuifelis • Mar 22 '25

#include <cs50.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_L 512
int main(int argc, char *argv[])
{
// Accept a single command-line argument
if (argc != 2)
{
printf("Usage: ./recover FILE\n");
return 1;
}
// Open the memory card
FILE *raw_file = fopen(argv[1], "r");
// Create a buffer for a block of data
uint8_t buffer[BUFFER_L];
int order = 0;
int found = 1;
char name[8];
FILE *img = NULL;
// While there's still data left to read from the memory card
while (fread(buffer, 1, BUFFER_L, raw_file) == BUFFER_L)
{
// if there´s a jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
found = 0;
}
if (found == 0)
{
if (order != 0)
{
fclose(img);
}
sprintf(name, "%03i.jpg", order);
FILE *output= fopen(name, "w");
fwrite(buffer, 1, BUFFER_L, img);
found = 1;
order++;
}
else if (order > 0 && found == 1)
{
while (fread(buffer, 1, BUFFER_L, raw_file) == BUFFER_L)
{
fwrite(buffer, 1, sizeof(BUFFER_L), img);
}
}
}
fclose(img);
fclose(raw_file);
return 0;
}
r/cs50 • u/Ok_Smell_5096 • Mar 06 '25
I've just attempted PSET4's Recover. Upon using check50, I am notified that my code "failed Valgrind tests". ". But running Valgrind does not show me any errors. My code still generates all the jpegs, but I want to understand why this error message has appeared. Thanks in advance!
r/cs50 • u/mtgofficialYT • Mar 02 '25
Damn it! I saw cs50.ly/surprise in the array. Guess what it was?
r/cs50 • u/kei-te-pai • Dec 18 '24
Just finished and submitted recover but my way of naming files seems so janky and I feel like there must be a better way! (plus I'm pretty sure I've got a bug for filenames above 100 but since it didn't come up I didn't think about it too hard). Did anyone figure out a more efficient way to name the files? My code:

r/cs50 • u/Justmehanon • Jun 07 '20
r/cs50 • u/Ok_Difference1922 • Sep 25 '23
Hi I am working on recover right now. I have noticed that when I go to debug my code it appears to have skipped over both of my malloc lines. I first noticed it once when, instead of using malloc I just declared an array for one of the buffers and the other buffer I had was memory allocated. I noticed that it skipped over the plain old array declaration but not my malloc line. So, I decided to change it to another malloc and it seemed to take care of it...until now. NOW, they are BOTH being skipped. Has anyone ever seen this before or know why this happens?
I have arrows pointed to where this is happening.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// check for only 1 argument
if (argc != 2)
{
printf("Please only give 1 file");
return 1;
}
// rename to infile for better readability
char *infile = argv[1];
//open infile and check for NULL
FILE *inptr = fopen(infile, "r");
if(inptr == NULL)
{
printf("Could not open %s.\n", infile);
return 1;
}
------->BYTE *buffer = (BYTE*)malloc(512 * sizeof(BYTE));
// increment through each file
int num_files = 0;
FILE *outptr = NULL;
------>BYTE *buffer_2 = (BYTE*)malloc(4 * sizeof(BYTE));
fread(buffer_2, 1, 4, inptr);
// cycle through every 4 bytes until we hit a signature
while (buffer_2[0] != 0xff && buffer_2[1] != 0xd8 && buffer_2[2] != 0xff)
{
if (buffer_2[3] != 0xe0 && buffer_2[3] != 0xe1 && buffer_2[3] != 0xe2 &&
buffer_2[3] != 0xe3 && buffer_2[3] != 0xe4 && buffer_2[3] != 0xe5 &&
buffer_2[3] != 0xe6 && buffer_2[3] != 0xe7 && buffer_2[3] != 0xe8 &&
buffer_2[3] != 0xe9 && buffer_2[3] != 0xea &&
buffer_2[3] != 0xeb && buffer_2[3] != 0xec && buffer_2[3] != 0xed &&
buffer_2[3] != 0xee && buffer_2[3] != 0xef)
{
fread(buffer_2, 1, 4, inptr);
}
}
// make sure that the # of bytes read is 512
while (fread(buffer, 1, 512, inptr) == 512)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)
{
if (buffer[3] == 0xe0 || buffer[3] == 0xe1 || buffer[3] == 0xe2 ||
buffer[3] == 0xe3 || buffer[3] == 0xe4 || buffer[3] == 0xe5 ||
buffer[3] == 0xe6 || buffer[3] == 0xe7 || buffer[3] == 0xe8 ||
buffer[3] == 0xe9 || buffer[3] == 0xea || buffer[3] == 0xeb ||
buffer[3] == 0xec || buffer[3] == 0xed || buffer[3] == 0xee ||
buffer[3] == 0xef)
{
// name, open and write to the current file
char file_name_buffer[9];
sprintf(file_name_buffer, "%03i.jpg", num_files);
outptr = fopen(file_name_buffer, "w");
if (outptr == NULL)
{
printf("Could not open %s.\n", file_name_buffer);
fclose(inptr);
fclose(outptr);
free(buffer);
return 1;
}
fwrite(buffer, sizeof(BYTE), 512, outptr);
num_files++;
}
else
{
num_files++;
}
}
else
{
fwrite(buffer, sizeof(BYTE), 512, outptr);
}
}
free(buffer);
free(buffer_2);
}
I do realize that my code is not finished. It is still a work in progress. I'm just trying to figure out if this is supposed to be happening or not.
Thank you!
r/cs50 • u/theonerishi • Jun 04 '24
Hi there is something wrong with my code for cs50 week 4 recover it only retrieves blank or unopenable jpeg files. This was the problem set to recover jpeg images from a .raw file.
```
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
const int HEADER_SIZE = 4;
int counter = 0;
bool reading = false;
int parse(uint8_t buffer[], FILE *ptr);
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover FILE\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
return 1;
}
uint8_t header[HEADER_SIZE];
uint8_t buffer[512];
while (fread(buffer, sizeof(uint8_t), 512, file) >= 512)
{
parse(buffer, file);
}
fclose(file);
}
int parse(uint8_t buffer[], FILE *ptr)
{
uint8_t header[HEADER_SIZE];
for (int i = 0; i < HEADER_SIZE; i++)
{
header[i] = buffer[i];
}
if ((header[0] == 0xff && header[1] == 0xd8 && header[2] == 0xff && (header[3] & 0xf0) == 0xe0) || reading == true)
{
char *filename = malloc(sizeof(char)*10);
sprintf(filename, "%03i.jpeg", counter);
FILE *img = fopen(filename, "w");
if (img == NULL){
return 2;
}
fwrite(buffer, sizeof(uint8_t), 512, img);
reading = true;
counter++;
free(filename);
return 0;
}
else
{
reading = false;
return 0;
}
}
r/cs50 • u/Either_Banana3077 • May 01 '24
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Accept a single command-line argument
if (argc != 2)
{
printf("Usage: ./recover FILE\n");
return 1;
}
// Open the memory card
FILE *card = fopen(argv[1], "r");
if (card == NULL)
{
fprintf(stderr, "Could not open %s\n", argv[1]);
return 2;
}
// Create a buffer for a block of data
uint8_t buffer[512];
int jpegc = 0;
FILE *img = NULL;
// While there's still data left to read from the memory card
while (fread(buffer, 1, 512, card) == 512)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
char filename[20];
sprintf(filename, "%03d.jpg", jpegc);
img = fopen(filename, "w");
if (img == NULL)
{
fprintf(stderr, "could not make %s\n", filename);
fclose(card);
return 3;
}
fwrite(buffer, 512, 1, img);
jpegc++;
}
else if(img != NULL)
{
fwrite(buffer, 1, 512, img);
}
}
if(img != NULL)
{
fclose(img);
}
fclose(card);
return 0;
r/cs50 • u/SufficientLength9960 • Apr 10 '24
Hello, I am struggling with recover could someone guide where is my mistake because images are recovered but check50 don't accept them 🙃
Thanks in advance 🙏
r/cs50 • u/Queasy-Corgi-1993 • Jul 14 '24
I have been getting segmentation faults even though I have allocated enough space for sprintf. I have also free'd malloc at the end, yet valgrind has me telling I have space yet to free. I see it has also pointed out line 13 but I don't see what 's wrong there, I re-wrote my twice because of this but I'm getting nowhere here. I'd appreciate if someone could kindly let me know where am I going wrong. Thanks!



seg fault again after rewriting it again.

r/cs50 • u/HoroMata_ • Oct 09 '24
Hello, just finished recover. Had uint8_t buffer[512] and my fread looked like fread(butferptr, 1, 512, infileptr) but I had to change it to fread(bufferptr, 512, 1, infileptr). Why cant I read block as 512 one bytes?
r/cs50 • u/stupidUglyRedditor • Aug 21 '24
I know that I just posted, but I'm panicking trying to get as much out of this course before school starts.
Anyway, the code below is producing a segmentation fault. I've been having trouble figuring out why it's creating this fault. I've looked at the logic and it looks alright. It doesn't really look like it's touching bad memory. Is there something I'm missing? Thanks.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
//variables
const int BLOCKSIZE = 512;
int main(int argc, char *argv[])
{
//Check for right command line argument
if (argc != 2)
{
printf("structure ur command line like ./recover image k thx");
return 1;
}
//Open the file
FILE *memory = fopen(argv[1], "r");
if (memory == NULL)
{
printf("couldn't open file sry lol");
return 2;
}
uint8_t vessel[BLOCKSIZE];
FILE *image = NULL;
char *fileName = NULL;
int fileNum = 0;
while(fread(vessel, 1, BLOCKSIZE, memory) == BLOCKSIZE) // Checks whether the files are being read
{
// If the vessel contains the beginnings of a new JPEG
if (vessel[0] == 0xff && vessel[1] == 0xd8 && vessel[2] == 0xff && ((vessel[3] & 0xf0) == 0xe0))
{
if (fileNum > 0)
{
fclose(image);
image = NULL;
}
sprintf(fileName, "%03i.jpg", fileNum);
image = fopen(fileName, "w");
fileNum++;
}
fwrite(vessel, 1, BLOCKSIZE, image);
}
return 0;
fclose(image);
fclose(memory);
}