10 votes
Accepted

How does “RGBTRIPLE triple;” know whitch pixel to store?

You have mixed them a little in your head but we are here. :) Firstly, the line RGBTRIPLE triple; doesn't get a pixel, it just declares a variable, of type RGBTRIPLE with the name triple. It would ...
ChrisG's user avatar
  • 7,396
5 votes
Accepted

Does fread() really read from the disc the size and the number of times as it is told?

Abstract: Disk I/O is complex, fread() provides a simpler interface to that complexity. This answer will take a look at what fread() does, and a further look at the things going on even deeper. ...
kluvin's user avatar
  • 1,223
2 votes
Accepted

pset4, recover.c: difference between two sets of fread() parameters

Are you sure that you haven't switched the working version for the non-working one? fread() requires 4 parameters. Because of your placement of parentheses, the second one only has one parameter, ...
Cliff B's user avatar
  • 68.8k
2 votes
Accepted

In pset6 server.c, does using realloc() reset the file pointer for fread()?

per fread's man page: size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by ...
kzidane's user avatar
  • 17.7k
2 votes

Segmentation fault in recover.c of pset4

FILE* img = fopen(title, "w"); // SEGMENTATION FAULT OCCURRING REALLY HERE This is because char title [3] does not have enough space to store the title, change it to char title [8] for example
MARS's user avatar
  • 5,169
2 votes

PSET 4 Recover Help with reading 512 byte blocks.

Lets look at some info provided in the Problem Set: JPEG signature: 0xff 0xd8 0xff 0xe0-0xef Each photo is stored immediately after the previously taken photo The image will be formatted with the FAT ...
Aternus's user avatar
  • 121
2 votes
Accepted

problem with recover.c using malloc and fread

You just opened your file for writing (and reading). This erased your card.raw's content. The 0 you find in block[0] is the initial value of your allocated memory, which by coincidence is 0. The 255 ...
Blauelf's user avatar
  • 20.9k
1 vote

How to write a fread function

I was researching on this as well and found this page with a comprehensive answer here. sizeof(BYTE) is equivalent to 1 so the second and third "variations" are the same. Both first and second ...
Pixces's user avatar
  • 13
1 vote
Accepted

Help with pset5 'recover': incomplete jpgs

Let's take a careful look at the code that is supposed to process all the data blocks that follow the signature block: //else if the block is NOT the start of a jpg else { //if ...
Cliff B's user avatar
  • 68.8k
1 vote
Accepted

recover.c:43:34: error: use of undeclared identifier 'out' fwrite(temp, 1, 512, out);: WHAT?

The variable out is a local variable and in C the local variables are block scoped and hence the variable out in this case only exists within the block; { sprintf(fname, "%03i.jpg", fileCount); ...
Ashen Gunaratne's user avatar
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. ...
Blauelf's user avatar
  • 20.9k
1 vote

Recover fraed()

I might not be the best to help but I can try. I see when you call fread like this you are using infile 2 times in the arguments. fread(infile, 1, 512, infile); The first argument should be a array ...
JohnWick's user avatar
  • 155
1 vote
Accepted

PSET4 Recover images not showing correctly

The logic of the program has some serious flaws. While individual statements are functioning correctly, its the overall logic that isn't working. Go back and look at what the program is doing. Read ...
Cliff B's user avatar
  • 68.8k
1 vote
Accepted

PSET 4 Recover Help with reading 512 byte blocks.

Don't use int buf[128]; but uint8_t buf[512];, or BYTE buf[512];, or unsigned char buf[512];. With int, the first four bytes would be stored together in buf[0], and your test would have to be ...
Blauelf's user avatar
  • 20.9k
1 vote
Accepted

pset 4 - recover - fread keeps returning 0 when reading 512 bytes on buffer?

I tested with your code and it's definitely correct, as @cliff has said in the commment. Do make sure the file you are opening actually has more than 512 bytes. Perhaps your rawfile has been ...
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

pset4 resize - can't write all the characters from infile to outfile

There is "\n" (new line) char in the end of the first raw. So there are 33 chars, while you read-write only 32.
obesman's user avatar
  • 1,139
1 vote
Accepted

Pset4 recover using fread as condition

Remove the for loop with the extra freads. You do that in the condition already, so you'd read two blocks, discarding the first. &buffer[0] is actually same as buffer.
Blauelf's user avatar
  • 20.9k
1 vote
Accepted

Code Seems Correct but Output is garbage

When the program has opened the card.raw file and is reading into the buffer, once the program has completed one loop iteration, how does it know to read the next block of bytes and not just the ...
Yuri Laguardia's user avatar
1 vote
Accepted

Recover Pset4 - Getting 24 images

The code does two fread() calls at the top of the while loop. The data from the first fread isn't being processed, it's just being overwritten when the loop comes back around. This means that half the ...
Cliff B's user avatar
  • 68.8k
1 vote
Accepted

recover.c. basics. How do I get started with fread?

You're on the right track. You don't need to malloc()... it can be done with an array just like you declared... now you fread() 512 bytes into that array and check the first four bytes with temp[0], ...
Yuri Laguardia's user avatar
1 vote
Accepted

A question about the fread function

Simply put, the pointer will automatically be moved to point at the first unread byte past whatever was read in that particular statement, whether it was 1 byte or 512 bytes, or anything else. If you ...
Cliff B's user avatar
  • 68.8k
1 vote
Accepted

pset4 resize: how to copy a row

"Everyone's looking for a zebra. I see the injured horse." All of the file reads and writes are using the outptr output file pointer. The only reference to the input file pointer inptr is the fclose ...
Cliff B's user avatar
  • 68.8k
1 vote

Pset4, Recover, GDB on fread gives an "Coud not open file" error and segfault

The problem was fileName array, it needs to be 8, not 7. sprintf was overwriting important memory because of that.
qua11q7's user avatar
  • 11
1 vote

pset6 load: fread returns 0

From man fread (emphasis added): On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when ...
DinoCoderSaurus's user avatar
1 vote

Query on fread (or fwrite)

The most obvious difference is the return value: fread returns the number of elements read (in this case, the number of sizeof(int)'s). If all goes well, the first formulation will return 10, while ...
ronga's user avatar
  • 1,415
1 vote
Accepted

pset4 resize My triple variable changes after fwrite

Your triple didn't change, it moved. You have two significant problems. Your headers are wrong and your padding is wrong. You need to get the headers right before moving on to the images. I recommend ...
Cliff B's user avatar
  • 68.8k

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