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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
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, ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 18.4k
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 ...
user avatar
  • 5,172
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 || ...
user avatar
  • 20.7k
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 ...
user avatar
  • 20.7k
2 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 ...
user avatar
  • 5,172
2 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 ...
user avatar
  • 64.2k
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, ...
user avatar
  • 5,172
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, ...
user avatar
  • 64.2k
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.
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 ...
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
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, &...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 20.7k
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 20.7k
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 ...
user avatar
  • 20.7k
1 vote

Pset4 recover- cannot open file for writing error

Also, newfile needs to contain the name of the file to be opened. It never has the filename assigned to it.
user avatar
  • 64.2k
1 vote
Accepted

Pset4 recover- cannot open file for writing error

int p=0; char *newfile = malloc(sizeof(buffer)*p); You actually have zero bytes reserved with malloc
user avatar
  • 5,172
1 vote
Accepted

Why do the actual images not show up in my image output?

You have to check the first 4 bytes for every new JPEG and not just the first. Then if you have a new JPEG, you should open a new file, write the data to it. If the next block is not a new JPEG, then ...
user avatar
1 vote
Accepted

Pset4 recover invalid or unsupported

At first read PSET4 RECOVER Can't Open JPEGS, I Have Tried Everything Please HELP! about sizeof(buffer) Then, do something with the whole logic of your code. Write it in your language (english or ...
user avatar
  • 1,139
1 vote
Accepted

Recover - "recovered photo does not match"

Hmmmm..... fwrite(&inptr, 512, 1, img); What's the name of your buffer? BTW, in case you were thinking about it, fwrite will not write directly from one file to another. It only writes ...
user avatar
  • 64.2k
1 vote
Accepted

Seg fault after comparison `(card_r[0] == 0xff)` of bytes in Recover pset4, and question about fread in a while loop

You should read 512 bytes, not 512 ints. A type holding a byte could be the BYTE alias, or unsigned char, or, my preference, uint8_t. And yes, a single fread in the loop head should be sufficient. ...
user avatar
  • 20.7k
1 vote
Accepted

Recover pset : cannot manage to check jpg signature

Shouldn't your buffer be an array of 512 bytes? (unsigned chars)? You've declared an array of integers.
user avatar
  • 18.4k

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