Thanks for the great explanation. The book I'm reading says that fseek() and ftell() are not guaranteed to work with text files as with binary files so I had to come see if your channel had a video. Excellent as always. Hoping for an ftell() video soon!
its probably a year or 2 since this video but can we specify prefix numbers if its either text or binary like 0x15 or whatever like that? also i see that fprintf is writing to a file huh? maybe it different for binary one like fwrite?
Maybe is more uncomfortable but, since the file handle is a pointer, could we do this of moving the position with pointer aritmethic too? (tho I would never)
I've never tried that, I don't think that's something that can be done unless a compiler decided to support it somehow: stackoverflow.com/questions/57897105/incrementing-and-decrementing-a-file-pointer.
thank you for that explanation but i have a question, are there any differences between rewind() and fseek() for when you want to place cursor at the beginning?
From the Ubuntu man page for fseek: The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to: (void) fseek(stream, 0L, SEEK_SET) except that the error indicator for the stream is also cleared (see clearerr(3)).
Great question Alexis. I don't have a video on ftell() yet, so maybe an online resource like this can help you in the meantime: www.tutorialspoint.com/c_standard_library/c_function_ftell.htm. :-)
@@PortfolioCourses from the Ubuntu man page for fseek: The rewind() function returns no value. Upon successful completion, fgetpos(), fseek(), fsetpos() return 0, and ftell() returns the current offset. Otherwise, -1 is returned and errno is set to indicate the error.
Could i ask, how do you count for if you fseek(file, -50, SEEK_END) but the file does not have any byte from 50 from the end? I found that c=fgetc(file) != EOF doesnt catch the error here 😭
It should set ferror if this occurs: en.cppreference.com/w/c/io/fseek. And then you could use ferror(file_pointer) to detect the error, e.g.: en.cppreference.com/w/c/io/ferror. :-)
Thanks for the great explanation. The book I'm reading says that fseek() and ftell() are not guaranteed to work with text files as with binary files so I had to come see if your channel had a video. Excellent as always. Hoping for an ftell() video soon!
best explanation ever sir...
Thank you Carlito! 😀
thank you sir really easy to understand love from suramadu
You're welcome Vinaldo! :-) I'm really glad you found it easy to understand!
but why it prints only 2 asterisks intead of 4 at the end??
Why 'j' isnt repleased by '*'?
We only print two asterisk chars with:
fprintf(file, "**");
So that is all we can expect to appear.
The end of the file looks like this:
...
g
h
i
j
EOF
@@PortfolioCourses thanks!!
its probably a year or 2 since this video but can we specify prefix numbers if its either text or binary like 0x15 or whatever like that? also i see that fprintf is writing to a file huh? maybe it different for binary one like fwrite?
Maybe is more uncomfortable but, since the file handle is a pointer, could we do this of moving the position with pointer aritmethic too? (tho I would never)
I've never tried that, I don't think that's something that can be done unless a compiler decided to support it somehow: stackoverflow.com/questions/57897105/incrementing-and-decrementing-a-file-pointer.
thank you for that explanation but i have a question, are there any differences between rewind() and fseek() for when you want to place cursor at the beginning?
From the Ubuntu man page for fseek: The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to:
(void) fseek(stream, 0L, SEEK_SET)
except that the error indicator for the stream is also cleared (see clearerr(3)).
the goat 🐐🔥
Fseek returns 0 when operation is successful
thank you so much !
You’re very welcome! :-)
Hi sir, can I ask how to use ftell? As it might relate to fseek :)
Great question Alexis. I don't have a video on ftell() yet, so maybe an online resource like this can help you in the meantime: www.tutorialspoint.com/c_standard_library/c_function_ftell.htm. :-)
I've often read that fseek() returns zero if it succeeds. Now I'm bit confused what is right. But thanks for the video.
Online sources indicate that it returns zero if it succeeds too, I'll have to look into this.
@@PortfolioCourses from the Ubuntu man page for fseek: The rewind() function returns no value. Upon successful completion, fgetpos(), fseek(), fsetpos() return 0, and ftell() returns the current offset. Otherwise, -1 is returned and errno is set to indicate the error.
Could i ask, how do you count for if you fseek(file, -50, SEEK_END) but the file does not have any byte from 50 from the end?
I found that c=fgetc(file) != EOF doesnt catch the error here 😭
It should set ferror if this occurs: en.cppreference.com/w/c/io/fseek. And then you could use ferror(file_pointer) to detect the error, e.g.: en.cppreference.com/w/c/io/ferror. :-)