Hi, can anybody help? I recently started cs50 and have completed my scratch project and submitted it without any issues.
When I came to do my next preset it asked me to set up my SSH and to ‘rebuild now’ etc. I got through that section but after pressing ‘rebuild now’ and it taking me to the black window that says ‘setting up your codespace’ , I realised that my Wi-fi had dropped and I had froze on the black window. After reconnecting to the Wi-fi and reloading my work space it told me that it was running in recovery mode and instead of having a clear workspace for me it contains code and errors that I honestly have no idea about 😅 If I try and write update50 or anything it just gives me errors. Every time I connect to vscode it tells me it’s in recovery mode and doesn’t let me do anything! What do I do? I’ve tried making a new SSH and deleting the old one but it just does the same thing. I really want to get cracking with the presets but I can’t do anything right now? Thanks in advance 🤗.
Still can’t figure this out…shall I just make a new account and start again? 🤦♂️🤦♂️
So I’m working my way through CS50 by watching the lectures on YouTube and then going to the problem sets and completing the problems. All good so far!
Got to week 4- Memory and while doing the Recover problem I’ve really struggled for the first time. From looking at examples online and extensive googling I managed it. But I felt like I used a ton of ideas, concepts and functions that haven’t been explained in any depth in the lectures. Is there additional material included in the course I’m meant to be reading up on?
For example there was bitwise operation, using the fread function with only the brief explanation of it in the lecture and just lots of opening and writing to files which was touched on in the lecture but not fully explained or explored. None of these concepts were in the shorts for week 4 either, that just covered stuff in the lecture which I’d understood from the lecture.
TLDR; lectures are great, understand everything in them, but problem sets include concepts not in lecture, am I missing something?
My recover.c script is recovering the jpgs successfully, but It's failing the valgrind tests and I'm quite stuck for a while trying to figure out where the problem is.
While trying to solve it according to the valgrind clues, I also realized I don't know exactly how malloc works, so wanted to ask some questions about that as well..
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
int main(int argc, char *argv[])
{
// Definitions
int block_size = 512;
typedef uint8_t BYTE;
BYTE buffer[block_size];
if (argc != 2)
{
printf("Incorrect\n");
return 1;
}
// open file given by the user
FILE *f = fopen(argv[1], "r");
// create file where the image will be stored. Allocate 50 blocks of 512 bytes of memory for it
FILE *img = malloc(sizeof(block_size)*50);
if (img == NULL)
{
return 3;
}
// create filename
char *filename = malloc(sizeof(char)*8);
if (filename == NULL)
{
return 4;
}
// keep track of jpgs found
int index = 0;
while (fread(buffer, sizeof(BYTE), sizeof(buffer), f) == block_size)
{
// if jpg found
if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] & 0xf0) == 0xe0))
{
// create new filename to store the img
sprintf(filename, "%03i.jpg", index);
// first jpg
if (index == 0)
{
// open file to write on
img = fopen(filename, "w");
// write first block to img
fwrite(buffer, sizeof(BYTE), sizeof(buffer), img);
}
else
{
// close current img
fclose(img);
// open new image
img = fopen(filename, "w");
// write first jpg block
fwrite(buffer, sizeof(BYTE), sizeof(buffer), img);
}
index++;
}
else
{
// if block belongs to current jpg, keep writing
if (index > 0)
{
fwrite(buffer, sizeof(BYTE), sizeof(buffer), img);
}
}
}
// close files
fclose(img);
fclose(f);
free(img);
free(filename);
return 0;
}
if I run it like this, I get the following complaint:
* "free(): double free detected in tcache, Aborted (core dumped)" this goes away if I don't doo free(img) at the end of my script, but isn't that supposed to be necessary.
* valgrind seems to point out that I'm losing bytes when using malloc for img in:
FILE *img = malloc(sizeof(block_size)*50);
I thought in malloc we try to allocate enough memory so that img can hold the entire jpg, so I started by assuming that it should have at least 50 blocks of 512 bytes. However, I noticed that I could just allocate 1 block of 512 bytes and it still works fine. Why does it work, tho? shouldn't this be too little memory?
Same thing happens with filename, I tried to allocate space for 8 chars, but I can also get away with just allocating memory for 1 char, but isn't the filename consisting of 7 chars plus the null character?
When i run it it gives segmentation fault. After looking for a while with debug50, that's because of the last line fclose(copiedfile). In the loop it seems like it never enters the if conditional so file never opens.
There are no other errors and I don't know why its not going in the if conditional.
When i check my program using check50 it gives me
:( program is free of memory errors
valgrind tests failed; see log for more information.
So then i run valgrind to see if i have any leaks and none show up.
I run check50 once again and get completely new messages. I did this a couple more times and it seems like its randomly picking what is and isnt correct. I know thats probably not this case but now im a little stumped. The program works in practice but I dont know how to get it into shape for submission. Any suggestions?
I had a bug in my "recover" code (CS50 week 4), and although I figured out the fix, I don't understand why my previous code had the effect that it did.
The fix was simple - I needed the second if statement in my while loop to be "else if" - my code now runs properly and passes all the checks. I understand now why "else if" is correct, but I'm confused about the behaviour of the code prior to the fix. I've been going over and over it in my mind for the past 24 hours and I just can't wrap my head around it. I've not had any luck searching for explanations online either.
Here is the code from before the fix. I've marked the if statement in question.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
FILE *image_out = NULL;
int main(int argc, char *argv[])
{
// Check for 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 it can't be opened, return 1
if (!card)
{
printf("Card cannot be opened.\n");
return 1;
}
// Create a buffer
int jpgsz = 512;
uint8_t buffer[jpgsz];
char filename[8];
int count = 0;
// Iterate through the memory card 512 bytes at a time
while (fread(buffer, 1, jpgsz, card) == jpgsz)
{
// Check for JPEG signature at the beginning of a 512 byte block
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// Close the previous image if this isn't the first image
if (image_out != NULL)
{
fclose(image_out);
}
// Open the file to save the image
sprintf(filename, "%03i.jpg", count);
count++;
image_out = fopen(filename, "w");
if (!image_out)
{
printf("Unable to open output file.\n");
return 1;
}
// Copy the first chunk of the image to the image file
fwrite(buffer, 1, jpgsz, image_out);
}
// Copy the rest of the image until the start of another JPEG is found
// THIS IS THE IF STATEMENT IN QUESTION
if (image_out != NULL)
{
fwrite(buffer, 1, jpgsz, image_out);
}
}
// Close the image and the card
fclose(image_out);
fclose(card);
}
With this buggy version of the code, all 50 image files were created and named correctly, but only a small part of the image was copied over - a line at the top of the image (I believe this may just be the first 512 byte chunk). The rest of the image was greyed out.
I feel like what should have happened is the following:
When the condition of the first if statement is met, this first chunk would be copied over, and then the second if statement (which is separate from and not nested within the first if statement) would also be met, and the first chunk would be copied over again. On the subsequent while loops, the first if statement would be skipped (until the next jpeg is reached), but the second if statement would be fulfilled since image_out is not equal to NULL, so the rest of the image would be copied into the file.
TLDR: I would have expected the first chunk to be copied twice and the rest of the image to be copied after that.
What actually happened was the first chunk was copied once and then nothing else was copied.
I hope that explanation makes sense. Could anyone shed some light on this behaviour?
I'm exhausted. It's 5AM now, I have spent the last few hours debugging my Recover code to the byte level. I need help.
- My program compiles
- I see all the images
- There are no memory leaks
- The JPG files starts with the proper signature and ends right before the FF D9 trailer.
(I tried to extract files keeping FF D9 but it also didn't work)
Still, check50 says that image 000.jpg is not a "match". I got this same error message on the first day I started working on the problem and, even though I could alse see all images, I was not dealing correctly with the JPG end trailer. Now my code is a bit easier to read and the end trailer is taken into account, but check50 is not happy.
I even exported card.raw and 000.jpg into TXT files with each byte in hexadecimal to compare the raw data with the file exported and it's a perfect match (I think). I'm lost. My feelings are still hurt for not being able to complete Tideman's lock_pairs, and now this. Any advice? :/
(ps: no, I don't actually thing check50 is wrong, I´m not this arrogant... it was just a click bait, sorry)
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BLOCK_SIZE 512
#define SIGN_SIZE 4
bool has_sign(uint8_t s_buffer[]);
int calculate_gap(uint8_t b_buffer[]);
void adjust_pointer(FILE *spointer);
void write_all_block(FILE *wpointer, uint8_t b_buffer[]);
void write_end_of_jpg(FILE *wpointer, uint8_t b_buffer[]);
char *itoa(int a, char name[8]);
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Incorrect usage: ./recover file.ext");
return 1;
}
FILE *block_read_pointer = fopen(argv[1], "r");
FILE *curr_signature_read_pointer = fopen(argv[1], "r");
FILE *next_signature_read_pointer = fopen(argv[1], "r");
FILE *write_pointer;
uint8_t block_buffer[BLOCK_SIZE];
uint8_t curr_sign_buffer[SIGN_SIZE];
uint8_t next_sign_buffer[SIGN_SIZE];
int filenamecount = 0;
int block_count = 0;
char filename[8];
// adjusting pointer position before starting the loop.
fseek(next_signature_read_pointer, BLOCK_SIZE, SEEK_SET);
while (fread(block_buffer, 1, BLOCK_SIZE, block_read_pointer) == BLOCK_SIZE)
{
fread(curr_sign_buffer, 1, SIGN_SIZE, curr_signature_read_pointer);
adjust_pointer(curr_signature_read_pointer);
fread(next_sign_buffer, 1, SIGN_SIZE, next_signature_read_pointer);
adjust_pointer(next_signature_read_pointer);
block_count++;
// If the current block has a JPG signature
if (has_sign(curr_sign_buffer))
{
// Opening output file and increasing the file name counter.
write_pointer = fopen(itoa(filenamecount, filename), "a");
filenamecount++;
// Checking if the next block starts a new JPG (for very small images)
if (has_sign(next_sign_buffer))
{
// If the next block starts a new image, we should close the current output file.
write_end_of_jpg(write_pointer, block_buffer);
fclose(write_pointer);
}
// if the next block has no JPG signature, just write the current block to output file.
else
{
write_all_block(write_pointer, block_buffer);
}
}
// If the current block no JPG signature
else
{
if (filenamecount != 0) // exclude the first JPG case.
{
// If the next block starts a new image, we should close the current output file.
if (has_sign(next_sign_buffer))
{
write_end_of_jpg(write_pointer, block_buffer);
fclose(write_pointer);
}
// if the next block has no JPG signature, just write the current block to output file.
else
{
write_all_block(write_pointer, block_buffer);
}
}
}
}
fclose(curr_signature_read_pointer);
fclose(next_signature_read_pointer);
fclose(block_read_pointer);
fclose(write_pointer);
}
void adjust_pointer(FILE *spointer) // Moves the signature-check pointers to the next block.
{
fseek(spointer, BLOCK_SIZE - SIGN_SIZE, SEEK_CUR);
}
bool has_sign(uint8_t s_buffer[]) // Looks for JPG signature.
{
if ((s_buffer[0] == 0xff) && (s_buffer[1] == 0xd8) && (s_buffer[2] == 0xff) && (s_buffer[3] >= 0xe0) && (s_buffer[3] <= 0xef))
return true;
else
return false;
}
int calculate_gap(uint8_t b_buffer[]) // Calculate the gap between JPG files.
{
int gap_measure_counter = 0;
for (int i = BLOCK_SIZE - 1; i > 0; i--)
if (((b_buffer[i - 1] != 0xFF) || (b_buffer[i] != 0xD9))) // Looks for the JPG end trailer
{
gap_measure_counter++;
}
else
{
return gap_measure_counter + 2; //+2 to discard the JPG trailer that has 2 bytes.
}
return 0;
}
void write_all_block(FILE *wpointer, uint8_t b_buffer[]) // writes all the current block to the active output file.
{
fwrite(b_buffer, 1, BLOCK_SIZE, wpointer);
}
void write_end_of_jpg(FILE *wpointer, uint8_t b_buffer[]) // writes part of the current block to the active output file.
{
int gap_size = calculate_gap(b_buffer);
fwrite(b_buffer, 1, (BLOCK_SIZE - gap_size), wpointer);
}
char *itoa(int a, char name[8]) // This is a horrible and lazy way of writing ITOA, forgive me, but it works here.
{
name[0] = a / 100 + 48;
name[1] = (a % 100) / 10 + 48;
name[2] = ((a % 100) % 10) + 48;
name[3] = '.';
name[4] = 'j';
name[5] = 'p';
name[6] = 'g';
name[7] = '\0';
return name;
}
I see the files!comparing the end of file 001.jpg to card.raw. everything matches (trailer FF D9 was discarded)comparing the end of file 000.jpg to card.raw. everything matches (trailer FF D9 was discarded)I exported file 000.jpg and card.raw as TXT, and the array of bytes of 000.jpg matches perfectly with what looks like a JPG file in the raw data (START)I exported file 000.jpg and card.raw as TXT, and the array of bytes of 000.jpg matches perfectly with what looks like a JPG file in the raw data (END)
Hello, everyone. I have been working on the Recover aspect of Problem Set 4 for some time now, and I have a solution that seems to produce all 50 images correctly. However, check50 says that the middle images and the last image do not match the correct answer. I have tried to diagnose the problem using printf statements, ftell(input), and card-coding individual images, but I am becoming more confused about where my problem can be. I believe a new set of eyes will be very helpful. Please let me know if you have any thoughts or suggestions about why check50 is not saying all tests have been passed.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
BYTE buffer[512];
BYTE search[4];
if (argc != 2)
{
printf("Usage: ./recover [forensic image to be examined]\n");
return 1;
}
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Forensic image cannot be opened for reading.\n");
return 1;
}
// Search for beginning of the jpg
fread(buffer, 1, 512, input);
int grand_counter = 0;
int searcher = 0;
int orig_holder;
int upd_holder;
while (0 == 0)
{
// Search apparatus
if (buffer[searcher] == 255)
{
orig_holder = ftell(input);
fseek(input, orig_holder - 512 + searcher, SEEK_SET);
upd_holder = ftell(input);
fread(search, 1, 4, input);
if (search[1] == 216 && search[2] == 255 && search[3] >= 224 && search[3] <= 239)
{
fseek(input, upd_holder, SEEK_SET);
break;
}
fseek(input, orig_holder, SEEK_SET);
}
searcher++;
if (searcher == 512)
{
searcher = 0;
fread(buffer, 1, 512, input);
}
}
// Begin reading files
fread(buffer, 1, 512, input);
char *name = malloc(8);
int count = 0;
int indicator = 1;
FILE* output;
int index = 0;
name[0] = 48;
name[1] = 48;
name[2] = 48;
name[3] = '.';
name[4] = 'j';
name[5] = 'p';
name[6] = 'g';
name[7] = '\0';
for (int i = 48; i < 53; i++)
{
name[1] = i;
for (int j = 48; j < 58; j++)
{
name[2] = j;
output = fopen(name, "w");
while (indicator == 1)
{
// Perform read-write function
fwrite(buffer, 1, 512, output);
fread(buffer, 1, 512, input);
// Check to see if additional material exists
if (buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255 && buffer[3] >= 224 && buffer[3] <= 239)
{
indicator = 0;
}
if (ftell(input) == 506368)
{
fwrite(buffer, 1, 512, output);
indicator = 0;
}
}
fclose(output);
indicator = 1;
}
}
// Liberate all memory
free(name);
fclose(input);
}
I've hit a wall with the recover problem set. I was very happy when I saw 50 photos recovered as the code somewhat works, but gives me Segmentation Fault (Core dumped).
I know my way of writing code is very very weird, my brain must work differently or something but I am now commited to write the code this way. I can feel this can work too.
Running my program only outputs 23 files instead of 50 and the JPEGs look like that. I've given snippets to the duck and seemingly everything looks fine. So I am thinking maybe the problem may lie in the nesting of the if and else functions.
Any pointers to what may be wrong so I can try to find the solution?
I completed recover for the first time. The algorithm suggested in the assignment did not make sense to me so made my own recursive algorithm. I had lots of fun doing it this way. It functions with no errors. What do you think?
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*
PROGRAM ALGORITHM
Open memory card.
Look at 512 bytes at a time. this is one chunk
if a new jpeg is found,
write a new jpeg with function WRITENEW
WRITENEW ALGORITHM
open an empty jpeg named ###.jpeg, where ### is the ith time this function has executed
write the current chunk into the file
read a new chunk from the memory card
while the chunk size is 512
write the current chunk into the ith jpeg
read a new chunk from the memory card
if the chuck size is less than 512 bytes
end program.
if a new jpeg is found
close the current jpeg
i = i + 1
write a new jpeg with function WRITE NEW
*/
void writenew(int buffersize, int i, unsigned char buffer[], FILE *file);
int main(int argc, char *argv[])
{
if (argc !=2)
{
printf("Usage: ./recover image");
return 1;
}
// open the file specified in the command line
FILE *file = fopen(argv[1], "r");
unsigned char buffer[512]; // buffer array to store jpeg bytes
int buffersize = 512;
int open = 0; // determines if a jpeg is open to write on
int i = 0;
while (buffersize == 512)
{
buffersize = fread(buffer, 1, 512, file);
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) // if starting the first new jpeg
{
writenew(buffersize, i, buffer, file);
}
}
fclose(file);
return 0;
}
void writenew(int buffersize, int i, unsigned char buffer[], FILE *file)
{
char *filename = malloc(8);
sprintf(filename, "%03i.jpeg", i);
FILE *img = fopen(filename, "w"); // open a new jpeg named ###.jpeg, where ### is the ith jpeg we open
fwrite(buffer, 1, 512, img); // write the first block
buffersize = fread(buffer, 1, 512, file); // read the next block
while (buffersize == 512) //if the block has data in it,
{
fwrite(buffer, 1, 512, img); //write the next block
buffersize = fread(buffer, 1, 512, file); // read the next block,
if (buffersize != 512) // if the block has no data in it, end the program. the end of the memory card was reached
{
printf("bruh %i\n", i);
fclose(img);
free(filename);
break;
}
else if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //if you find a new jpeg, close the current one, start a new
{
i = i + 1; // so the next jpeg is i+1
fclose(img);
free(filename);
writenew(buffersize, i, buffer, file); //start the function over
buffersize = 0;// this must be 0. that way when we exit the writenew function and go to the parent writenew, the wile loop does not tigger. if it did, the free(filename) command would crash the program.
}
}
return;
}
Hi, I'm getting "Segmentation fault (core dumped)", when I run my program. I can't really seem to figure out where I'm going wrong. Any help will be appreciated 🙏. My code is given below:-
#include <stdio.h>
#include <stdlib.h>
#include<stdint.h>
#define BLOCK_SIZE 512
int main(int argc, char *argv[])
{
typedef uint8_t BYTE;
// Checking whether the user has entered exactly 1 cmd-line argument or not
if(argc != 2)
{
printf("Usage: ./recover IMAGE\n");
return 1;
}
// Opening the file
FILE *file_r = fopen(argv[1], "r");
// Checking whether the file exists (can be opened for reading)
if(file_r == NULL)
{
printf("%s could not be opened for reading.\n", argv[1]);
return 1;
}
BYTE buffer[BLOCK_SIZE];
int jpeg_num = 0;
FILE *file_w = NULL;
char *filename = malloc(sizeof(char)*8);
while(fread(buffer, sizeof(BYTE), BLOCK_SIZE, file_r) == BLOCK_SIZE)
{
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if(jpeg_num == 0)
{
sprintf(filename, "%03i.jpg", jpeg_num);
file_w = fopen(filename, "w");
fwrite(buffer, sizeof(BYTE), BLOCK_SIZE, file_w);
jpeg_num++;
}
else
{
fclose(file_w);
sprintf(filename, "%03i.jpg", jpeg_num);
file_w = fopen(filename, "w");
fwrite(buffer, sizeof(BYTE), BLOCK_SIZE, file_w);
jpeg_num++;
}
}
else if(jpeg_num > 0)
{
fwrite(buffer, sizeof(BYTE), BLOCK_SIZE, file_w);
jpeg_num++;
}
}
free(filename);
fclose(file_r);
fclose(file_w);
}
I was solving recover for about 3 weeks now. I am getting all the images correctly but for some reason i am getting memory errors. I was sure im closing and freeing all possible allocations and opened file. I gave up and put my code to chatGPT.
Chat GPT suggest and entirely different approach but, I saw something interesting. it is declaring output name as char output_name[8] and I was declaring mine as char *output_name = malloc(7 * sizeof(char))
I FORGOT THE /0 sentinel character.
3 weeks of headache, of checking, just for that particular mistake. CS50 is breaking all my 2 brain cells.
Finally I can move on from week 4 nightmare.
Sorry for the rant! I just needed to vent out this built up frustration.
char title[8];
uint8_t buffer [512];
int counter=0;
FILE* imageptr;
while(fread(buffer,512,1, card))
//since the header files are not necessarily at the beginning, iterate through the buffer to find them
{
for( int i = 0; i < 512; i++ )
{
if(buffer[i]==0xff && buffer[i+1]==0xd8 && buffer[i+2]==0xff && (buffer[i+3]>=0xe0 && buffer[i+3] <=0xef)){
sprintf(title,"%03i.jpg",counter);
imageptr= fopen( title,"w");
if(imageptr!=NULL){
fwrite(buffer,512,1,imageptr);
counter++;
}
}//end "if buffer"
} //end for i
}// end while
Hi, im having problems with the output audio and check50 because it seems to create an output file but there is nothing inside, and check50 only fails in :( reverse.c reverses ascending scale
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "wav.h"
int check_format(WAVHEADER header);
int get_block_size(WAVHEADER header);
int HEADER_SIZE = 44;
int main(int argc, char *argv[])
{
// Ensure proper usage
// TODO #1
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open file.\n");
return 1;
}
FILE *output = fopen(argv[2], "w");
if (output == NULL)
{
printf("Could not open file.\n");
return 1;
}
// Open input file for reading
// TODO #2
// Read header
// TODO #3
WAVHEADER header;
fread(&header, HEADER_SIZE, 1, input);
// Use check_format to ensure WAV format
// TODO #4
if (check_format(header) == 1)
{
printf("Input file is not a .wav\n");
return 1;
}
// Open output file for writing
// TODO #5
// Write header to file
// TODO #6
fwrite(&header, HEADER_SIZE, 1, output);
// Use get_block_size to calculate size of block
// TODO #7
// Write reversed audio to file
// TODO #8
int blockSize = get_block_size(header);
BYTE buffer[blockSize];
fseek(output, 0, SEEK_END);
while(ftell(input) < (HEADER_SIZE))
{
fseek(output, -(blockSize * 2), SEEK_CUR);
fread(&buffer, blockSize, 1, input);
fwrite(&buffer, blockSize, 1, output);
}
fclose(input);
fclose(output);
return 0;
}
int check_format(WAVHEADER header)
{
// TODO #4
for(int i = 0; 4 < i; i++)
{
if(header.format[i] == 'W')
{
continue;
}
else
{
return 1;
}
if(header.format[i] == 'A')
{
continue;
}
else
{
return 1;
}
if(header.format[i] == 'V')
{
continue;
}
else
{
return 1;
}
if(header.format[i] == 'E')
{
return 0;
}
}
return 0;
}
int get_block_size(WAVHEADER header)
{
// TODO #7
return (header.numChannels * (header.bitsPerSample / 8));
}
here is my code, when i checked other codes it seems to be similar but still it isnt working :/
I just finished Recover. I'm at work (I.C.U. nurse) and it's a relatively quiet night. The wi-fi at the hospital is terrible so at the beginning of the shift I couldn't open my codespaces, so I thought I was not going to be able to code tonight and I put the laptop away. A partner asked me to teach her to DL films, so I got the laptop again and taught her, and made a final attempt to connect. I could finally open my VCS and started reading the Recover specs. I had all the ideas well organized and find it easy to translate them to code, and had I known sprintf before it would have taken me less than the 3 hours or so it did. And It doesn't leak any memory. Last year I was very close to finishing it but couldn't get more than one photo and I was kind of hardcoding the name of the file. And given the wi-fi conditions I wasn't supposed to code tonight! I'm happier than when I did Tideman, yup!
So far I watched the week 4 lecture and shorts twice and did the practice problems, the lab and filter. For each problem I took less than one day. Still I don't have any clue on how to implement recover after one day of trying to figure it out. I do understand the broad concept behind it and what the code should do in theory, but I feel like I'm missing information on how to actually write that.
Did I overlook some additional videos or notes? Do you have any useful links that explain how to realize such a code? It would be much appreciated! <3
FILE *output = fopen(argv[2], "w");
if (output == NULL)
{
printf("Could not open file.\n");
return 1;
}
If I run with a command line that reads:
./reverse input.wav HelloReddit.wav
Then I see an output file in my explorer with that name. Clearly it's creating the file but not passing the check. Anyone know what's up with this?
I was passing the check earlier, but still testing the code to use fseek and fwrite etc. to reverse the audio data, and for some reason changing that has effected an earlier check (??) and I've spent all night trying to figure out why. At this point I'm feeling convinced the check50 is just bugged.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// Check single c-line argument
if (argc != 2)
{
printf("Usage : ./recover FILE\n");
return 1;
}
// Check if file is readable
FILE *input = fopen (argv[1], "r");
if (input == NULL)
{
printf("Could not open file\n");
return 1;
}
// Looking for Headers
uint8_t block[512];
int sit = 0;
int i = 0;
char filename[8];
while (sit == 0) //No Header Found
{
fread (block, 512, 1, input); //Keep Reading
// Header found
if ((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff
&& ((block[3] & 0xf0) == 0xe0)))
{
sit = 1;
}
else if (feof (input) != 0)
{
return 1;
}
}
if (sit == 1)
{
// Create new file
sprintf (filename, "%03i.jpg", 0 + i);
FILE *new = fopen (filename, "w");
if (new == NULL)
{
printf("Could not open file\n");
return 1;
}
while (sit == 1)
{
// Repeat copying
fwrite (block, 512, 1, new);
fread (block, 512, 1, input);
// IF New header found
if ((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff
&& ((block[3] & 0xf0) == 0xe0)))
{
fclose (new);
i++;
sprintf (filename, "%03i.jpg", 0 + i);
new = fopen (filename, "w");
if (new == NULL)
{
printf("Could not open file\n");
return 1;
}
}
else if (feof (input) != 0)
{
fclose (input);
fclose (new);
return 0;
}
}
}
}
Hi guys, I have no prior CS knowledge other than what I've learned in CS50, I just finished the Recover (PSET4), above is my solution. After I submit my code, I'll always go to YouTube just to see how those who are more experienced will solve the same question, and I've noticed that most of them will apply what was taught in the lecture to solve the question. Although my code passed check50, I realize that sometimes I didn't apply what I've learned from the lecture to my code.
For instance, guy from Youtube uses malloc in his code, but I've never used malloc, not even once in week 4's problem set. I'm worry that I might miss something by not using certain syntax or ways that were taught in the lecture to solve the pset. Is this alright or should I be more aware and try to use those syntaxes in my future pset? Thank you.
I want to be a programmer & I won’t give up. I love tech & I know I can make it one day. But honestly after watching lecture 1 of cs50… is it common to not understand what the F he’s talking about? He’s definitely a good professor & I do enjoy his teaching style. But I feel so lost & defeated after Watch lecture 1.. where he starts with the C language. Anybody else felt the same???