Skip to main content

Questions tagged [fread]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
337 views

How can I find out the size of what i have just read with fread()?

currently my code looks like this. int finds_reading_length(FILE* the_open_file, int the_block_size) { fseek(the_open_file,-the_block_size,SEEK_CUR); BYTE* buffer[the_block_size + 1]; ...
0 votes
1 answer
137 views

"File size limit exceeded" in Recover - how does file writing work?

I've been quite frustrated by trying to make progress in Recover. When I run my code, I am unable to make even the first JPEG file as the program continues running until the file size is exceeded. I ...
0 votes
1 answer
18 views

Segmentation fault in pset4 recover when reading file

I tried solving recover.c. But when I tried to run it (without valgrind), I got segmentation fault. When I ran it again with valgrind, it told me that the culprit is in line 40, where I read the file ...
0 votes
1 answer
7 views

What is the return value of fread when it reads slack space?

This is my code for recover.c in pset4: #include <stdint.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("...
0 votes
1 answer
89 views

pset 4 resize.c algorithm problem

I can't figure out why my code wont work. Please help. My idea for solving the height problem was write one line of the image move the file cursor to the beginning of the newly written line read in ...
0 votes
1 answer
597 views

How to keep track of cursor in fread/fwrite etc

after two or more weeks I have finished recover problem set - the longest issue was with the cursor and debugging. I less than more know how to use debug50 but if somebody could tell me how can I ...
0 votes
2 answers
100 views

CS50 Recover - fread into buffer

I am working on CS50 Recovery and I am unable to get the program to check the JPEG starting header. I used debug50 and added a couple print statements to try and find the problem. Every time I run the ...
0 votes
1 answer
234 views

How can I know number of samples to add it as an argument for fread and fwrite?

I'm so confused about fread and fwrite. In lab 4 " Volume", how can I know the number of samples in the input wav file? i=I need to use fread(buffer , size , qty , file operator ). So how ...
0 votes
1 answer
56 views

Pset4 Recover. I think that I did well but it doesn't work

I was struggling with this code for three days. someone could help me? What should i fix? Please, if you are so kind, give me a tip, not the solution! I wrote a lots of comments to you can understand ...
0 votes
0 answers
322 views

fread() return value

I am working on recover, currently pset, and only my last photo is not processing properly. I am calling fread(buffer, 1, 512, inptr) to read 512 bytes of data at a time. The video says fread ...
0 votes
1 answer
373 views

How to write a fread function

Soo i'm a little confuse about the fread/fwrite function ... from what i understood in the videos, you need 4 input for those to work, let's use fread for the exemple fread("place to store the data",...
0 votes
1 answer
53 views

Help with pset5 'recover': incomplete jpgs

I´ve been working on recover for the past few days and I am having some trouble getting a working solution. The code below prints out 49 jpgs instead of 50, and they are all incomplete or very blurry. ...
0 votes
1 answer
287 views

problem with recover.c using malloc and fread

i tried to write a function that sample 4 bytes from card.raw file but i doesn't work because fread doesn't put any data into block buffer so i wrote this simple code and i found out that fgetc works ...
0 votes
1 answer
264 views

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

When I compile my program, clang is probably not working. I get this when I compile: ~/pset3/recover/ $ make recover clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -...
0 votes
1 answer
60 views

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

I have a question concerning: The part while (fread(card_r, sizeof(int), 512, inptr) == 512) I suppose I am reading into card_r here and don't need an additional fread statement for reading. I have ...
0 votes
1 answer
150 views

fread seems to break my program

I've been spending a lot of time trying to get my head around the fread and fwrite functions. In said (vain) attempts, I made some little changes to the copy.c program that is provided as part of ...
0 votes
1 answer
31 views

Recover fraed()

I am heandling right now recover side by side by example of resize and copy. I have some big problem that I do not understand with fread function: here is the code: //start the process fread(...
0 votes
1 answer
206 views

PSET4 Recover images not showing correctly

Here is updated code with answer. #include <stdio.h> #include <stdlib.h> #include <stdint.h> int main(int argc, char *argv[]){ if (argc != 2){ ...
0 votes
2 answers
3k views

PSET 4 Recover Help with reading 512 byte blocks.

I'm having some trouble trying to read and loop through the card.raw file. I'm not running into any errors though I can't figure out why when I call fread I continually get 0 as a return value. My ...
0 votes
1 answer
1k views

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

#include <stdio.h> #include <stdint.h> typedef uint8_t BYTE; int main(int argc, char* argv[]) { // checks proper user input on command line if (argc != 2) { fprintf(...
0 votes
1 answer
43 views

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

everyone! I really need your help. I'm working on pset4 and trying to understand the logic of resize problem. I have two files:testinp.txt and testout.txt. Inside of testinp.txt I have: ...
0 votes
1 answer
497 views

The function fread

How does fread actually work? In pset4 recover, i did this while (fread(buffer,sizeof(BYTE)*512, 1, inptr) == 1) { //If signature of first 4 elements is jpeg, create new file and write that ...
1 vote
1 answer
716 views

Pset4 recover using fread as condition

In the walkthrough Zamyla mentions using fread as a condition. I've tried using it as the while condition, hoping the loop would stop when fread returns 0 but I get only 26 heavily distorted jpegs ...
0 votes
1 answer
206 views

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

I have been getting segmentation fault on my code, when i try to debug it, it gives me this error: Error opening file Could not open file: /build/buildd/eglibc-2.19/libio/iofread.c The file could not ...
3 votes
1 answer
5k views

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

The declaration of fread is as following: size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); Code: char a[1000]; //1 fread(a, 1, 1000, stdin); //2 fread(a, 1000, 1, stdin); Will ...
0 votes
1 answer
300 views

Code Seems Correct but Output is garbage

Further to the question I posed yesterday, in which I couldn't compile as my declaration of buffer was incorrect. This has now been resolved as you can see in my code below, and the code will in fact ...
1 vote
1 answer
206 views

Questions: Is my small.bmp corrupted?/ How do I read the rgb values of each pixel from the ready outfile?

So this should be a little complex of questions. 1. I want to ask if my small.bmp file looks normal: I looked at it with the command "xxd -c 12 -g 3 -s 54 small.bmp" and the output was the same as ...
0 votes
1 answer
156 views

Recover Pset4 - Getting 24 images

First time on the site, I've been trying to do this pset by myself but as I write this thread, I have already spent easily more than 10 hours only in this code. I have reviewed it thoroughly once and ...
0 votes
1 answer
389 views

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

When it comes to recover.c for pset4, I am completely lost. fread(&data, size, number, input); To quote the video, "where &data is the pointer to a struct which will contain the bytes you’re ...
1 vote
1 answer
1k views

pset6 load: fread returns 0

So far, my server seems to be working correctly except when it comes to the step of loading a file into memory. I'm using fread to get the info. from file, and my goal is to store the number of bytes ...
1 vote
1 answer
118 views

Pset4 Recover: Not understanding where to put the forever loop to break out and close the image file

My code for recover.c works except for 015.jpg. I can see the image in the folder, but the file is not closing correctly and thus disrupting check50. I get that I want to put a "forever loop" ...
0 votes
1 answer
62 views

A question about the fread function

Does the pointer move one position further after the fread function has read a pixel (for example)? I will try to be more articulate with this example: fread(&triple, sizeof(RGBTRIPLE), 1, ...
0 votes
1 answer
175 views

pset5 resize file io fread

Among the recommended reading list for this pset was this page (http://www.cprogramming.com/tutorial/cfileio.html), which indicates that when using fopen specifically for reading or writing in binary, ...
2 votes
1 answer
3k views

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

I'm currently working on the cs50 pset4. My question is pretty straight forward: How does the line (marked with caps lock comment) "RGBTRIPLE triple;" knows whicht pixel currently to store? I get the ...
0 votes
1 answer
605 views

pset4 resize: how to copy a row

Just spent 2 days for figuring out how fread(), fwrite() and fseek() works. I tried to use "Rewrite" method from Zamyla's video for vertycal resizing: - Remember pixels in array (first written row of ...
0 votes
1 answer
54 views

Segmentation fault in recover.c of pset4

Below is the function createjpg() I wrote for recover.c of pset4. I have written void createjpg (void) { // opening card.raw FILE* inptr = fopen("card.raw", "r"); // ensuring that the desired file ...
0 votes
1 answer
189 views

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

The following was my load() function for pset6 server.c bool load(FILE* file, BYTE** content, size_t* length) { size_t count = 0; BYTE* file_char = malloc(sizeof(BYTE)); while (...
0 votes
1 answer
54 views

Query on fread (or fwrite)

Is there any difference between the below lines of code? fread(&buf, sizeof(int), 10,infile); and for (int i = 0, i < 10, i++) { fread(&buf, sizeof(int), 1,infile); } If so (or not ...
1 vote
1 answer
184 views

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

At first I could not get fread() to read anything from card.raw, but then I made a change to the second and third arguments, and the function worked. Although intuition led me to the change, the two ...
0 votes
1 answer
178 views

pset4 resize My triple variable changes after fwrite

My code is as follows: /** * resize.c * * Computer Science 50 * Problem Set 4 * * Resize a BMP. */ #include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, ...
0 votes
1 answer
207 views

pset4 recover: reading from infile

It compiles. But if I step into this function: fread(&pos, sizeof(BYTE), 1, file); I get an error from GDB: _GI_IO_fread (buf=0xbfffff0d, size=1, count=1, fp=0x804b008) at iofread.c:35 iofread.c:...
2 votes
1 answer
1k views

pset4 recover I'm confused with fread

I'm pretty confused with recover. I've seen in previous posts that it is wrong to use feof , but the problem is that when I use fread the images won't show. However, when I use feof I can see all ...
0 votes
1 answer
81 views

pset4: recover.c Why doesn't this work? It always "finds" 0 jpgs (There should be 16)

int tmp; //read from the file to tmp (int) while(fread(&tmp, 1, 1, inptr) == 1){ // if tmp is the first byte of jpg if(tmp == 0xff){ // if it can be read again ...
1 vote
1 answer
2k views

What is the difference in behavior between fread and feof?

For pset4's recover.c code, I noticed the following: when I set my while loop to be while (!feof(inptr)), all the images get recovered, including the last, but check50 throws an error (TODO) when ...
0 votes
1 answer
103 views

fread not updating the values in my struct in recover pset4

I created a struct typedef struct { BYTE buffer[512]; } __attribute__((__packed__)) BUFFER; then I try reading card.raw and writing to a new file in a while loop fread(&photo, sizeof(...
0 votes
1 answer
199 views

|Recover creates only 8 files | fread | pset4|

I'm stuck with recovery program in the pset4. My program recovers 8 files. I can not find out what is wrong with it... Here is the link on Dropbox to the code I'm using. If you notice any error ...
0 votes
1 answer
477 views

fread not putting what I expect in buffer

for Pset4, recover, I'm trying to read 4 bytes from card.raw into a buffer. I''m testing the first 4 bytes of card.raw with what my buffer contains using gdb, and I'm not getting the same thing. I ...
0 votes
2 answers
2k views

Why is fread second argument sizeof(x) and third 1?

I understand its meaning, per the man page, that function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the ...
0 votes
1 answer
793 views

fread reaches eof prematurely (pset 5)

Scratching my head on this one folks. My current program reads the first two chunks of 512 bytes of the first jpg (thats actually 3 reads total as the first 512 chunk is empty), but then fread fails ...
9 votes
2 answers
2k views

How does fread progress through each byte in copy.c?

In copy.c (and the other apps using the same start code), how does freed know to read one byte after another? It seems to just do it automatically. Eg, this will read each byte, from first until bi....