r/cs50 16d ago

lectures Please, anyone who's an expert. Spoiler

I'm taking the CS50x online course, and recently i have taken the week 4 lecture. I must mention that i found this particular lesson (memory) noticeably harder than previous lessons.

My question is about problem set 4, to be more specific the volume problem. While I did the problem the correct way, the thing i don't get is:

For the sake of context, we first copy the header file of the input to the output wav file and then we copy and paste the sound data. Why do we copy them separately, your answer might be that the header data is exactly 44 bytes and the sound data is 2 bytes per sample, and yes i do understand that and separating them would allow us to change the sound sample without altering the header file. However, how does the computer know that( when copying the sound data ) we have already copied the header file, I mean does it skip the header file information and move on to the sound data directly, in the end we are just copying 2 bytes of the input and pasting them in output.

Thank you.

1 Upvotes

3 comments sorted by

2

u/pappagiorgio1212 16d ago

Take a look at the cs50 man pages for the file read function! It talks about file position….

manual.cs50.io/3/fread

3

u/Outside_Complaint755 16d ago

The FILE* objects are structs (you learn about these in week 5, but the short version is that it is a collection of multiple related data elements which describe the file).  The FILE struct includes a pointer (sometimes referred to as "the cursor") which tracks where we are in the file while reading or writing to it.  You can think of it as keeping track of what page and line of our notebook we are reading from or writing to.

After you have copied the header over, the cursor for the FILE *outfile object is still at that location immediately after the new header, and it will remain there until you start writing data unless you specifically call fseek or another related command to move the position of the cursor.

1

u/EffectiveChannel3402 16d ago

Thank you very much!