Skip to main content
11 votes
Accepted

If I use fread in a while loop, will it move the file pointer?

When you do an fread, whether it is in a while loop statement or anywhere else, it will execute the read and will most definitely move the file pointer. The pointer will be repositioned to the first ...
Cliff B's user avatar
  • 69k
4 votes
Accepted

Recover pset 4 last image

It's a fairly common error with this pset. The last file is one 512 byte block too long. The logic structure of this program is: Read a block of data process the block of data check for EOF When ...
Cliff B's user avatar
  • 69k
4 votes
Accepted

PSet4 Recover, only finds 22 corrupted photos, doesn't pass Check50

This is a pretty common newbie mistake. Look at this code: //Loop until end of 512 block while (fread(buffer, sizeof(BYTE), 512, memorycard) == 512) { //Read 512 bytes into buffer fread(buffer, ...
Cliff B's user avatar
  • 69k
4 votes
Accepted

Why does the 0.49 jpg file doesn't match correctly?

This is a very common problem. ;-) Think carefully about how the while loop ends. It checks to see if the EOF flag is set. If it isn't, it loops again. The problem is that the EOF flag isn't set ...
Cliff B's user avatar
  • 69k
3 votes

PSET4, Recover - Generated JPGs are blank

I think you complicate your program unnecessarily with calls to functions that do not clarify the code, recover can be done in a simpler way, and personally I always look for the greatest possible ...
MARS's user avatar
  • 5,179
3 votes
Accepted

PSET4, Recover - Generated JPGs are blank

Mars is right, the code is too complex. Too much complexity around opening, closing, and testing for open files. It's understandable that when code is executed repeatedly, you'd want to create a ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET4 Recover Can't Open JPEGs in the IDE

Can you give me an example of a filename that is supposed to contain a line feed? sprintf(outfile, "%03i.jpg\n", foundPhoto); As a side note, a programming tip. When you see to nearly ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

recover pset4 file handling

1 It's not a single char. You've declared an array of 8 chars, which is what you need to hold "000.jpg" {'0', '0', '0', '.', 'j', 'p', 'g', '\0'} 2 bookmark is an array. An array will "decay" to a ...
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

PSet4 recover: recovered files start with a wrong sequence

The whole problem is given by the following statement: char fileName[7]; The file name is given by seven characters, but you must bear in mind that it is a string, so we need an additional character ...
MARS's user avatar
  • 5,179
2 votes
Accepted

pset4 recover.c Why are my pictures scrambled?

De Morgan's laws The opposite of buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0 is buffer[0] != 0xff || ...
Blauelf's user avatar
  • 21k
2 votes

Can't open generated JPGs

In fwrite(inPointer, 1, number, newFilePointer);, inPointer should be buffer, you don't want to write the content of the FILE structure to disk, but the bytes you read. Interesting part is - your ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

PSET4 Recover spits out 50 images but they're all invalid

Effectively your algorithm is correct, but there is a small mess with the pointers, you declare a pointer to the buffer in which it will be written: uint8_t *buffer_ptr = malloc(FAT_BLOCK_SIZE); So, ...
MARS's user avatar
  • 5,179
2 votes
Accepted

pset4 recover 049.jpg recovered image does not match

Your last file has one extra block of data. Look at this code: //do untill end of file while (!(feof(rawCard))) { //read 512 bytes and look for JPEG in first 4 bytes fread(bytes, 512, 1, ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

Pset 4 - Fopen returns NULL

This "argv[1]" is the string literal argv[1] (because of the "s), and no such file name exists.
DinoCoderSaurus's user avatar
2 votes
Accepted

(pset4) Fatal error: glibc detected an invalid stdio handle

At line 46, you write to the outside of filename declared at line 27. Here is the link that you can debug errors like this by yourself. Memory access error: writing to the outside of a memory ...
stensal's user avatar
  • 450
2 votes
Accepted

Understanding the contents of the raw file

It looks like gibberish because the code is printing it as if it were ASCII char data. In reality, it is literally raw digital data. It doesn't translate to ASCII codes or anything else. Instead, it ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

Pset4 Recovery Produces Corrupt Null Files & Skips An Image

"Ready, fire, aim!" The problem lies here: jpg = fopen(filename, "w"); sprintf(filename, "%03i.jpg", numfound); The sprintf command builds the name of ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET4 Recover: Check50 shows error

It's a problem with processing the EOF condition on the input file. The code is structured so that it reads in 512 bytes, copies them out to the output file, and then checks for EOF. In other words, &...
Cliff B's user avatar
  • 69k
2 votes

CS50 Recover recovering corrupted images, and only 43 images

There are several issues in this code. First, look at how i is being incremented. Inside the code, it is incremented by 512 in several places, but it's also being incremented by 1 in the for loop ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

Code for Good ol' Recover returns all fifty images (000-049), but doesn't pass Check50

actually, the code seems to be creating 52 files, numbered 000 to 051, so something is amiss. Perhaps you should consider reworking the logic a little bit? Hint: why open an output file before the ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

in pset4, recover, why is the first jpg found treated differently from the other jpg files?

Half of the answer lies in the program spec. All the data in the file before the first signature is garbage data to be discarded. The other half should be simple. Before using a file pointer to open ...
Cliff B's user avatar
  • 69k
2 votes

CHECK50 Passes, but I Get 6/7 When Submitting

check50 is timing out because your program takes longer to run than is allowed. It comes down to this Your buffer is unsigned char *buffer = malloc(512); Your fread is fread(buffer, 1, sizeof(buffer), ...
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

Problem Set 4 - Recover (timed out while waiting for program to exit)

You’re only reading in 8 bytes at a time (size of buffer) and that’s probably too slow for check50. You should be reading 512 bytes each time.
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

Problem Set 4 Recover

The issue is that your code is written to specifically handle only the one raw file that is given to you as an example. It needs to handle any raw file. (check50 is testing your code with a ...
curiouskiwi's user avatar
  • 18.7k
1 vote

PSet 4 Recover works, but returns 1, not 0. HELP!

bmp.h doesn't exist in check50 for this assignment, so the file is never included. This leads to compile errors. You'll need to incorporate the definition of BYTE into recover.c, or use another ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset4 recover - jpgs in place, 0 is returned, but Check50 fails - segfault

The problem is due to a data overwrite bug. The problem lies in these two lines: char jpgName[4]; // array for 3 symbols with trailing 0 .... // give a name and write the 1st block ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

PSET 4 - RECOVER

You write first sector twice for each file. Remove first fwrite(buffer, 512, 1, img); And I'd put another if (img != NULL) in front of the second fclose(img); in case the dump contains no JPEG file ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

pset4 recover single image

A loop is definitely the way to go, not if statements. Now, have you looked at the size of the file(s) produced? I suspect that the first file created is much larger than it should be - a ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Problem Set 4 Recover Issues. I can't figure out why my code doesn't work

You have multiple fread, each reading 512 bytes, but not every read sector seems to be written even after opening a file. This means you're skipping some sectors. Instead of a do..while loop, use a ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

How to solve the segmentation fault in pset4 recover?

Since it should persist over multiple iterations, move FILE* photo = NULL; above your loop. Otherwise you're creating a new variable each iteration and set it to NULL, which makes fwrite fail. Second ...
Blauelf's user avatar
  • 21k

Only top scored, non community-wiki answers of a minimum length are eligible