Reading and Writing Files in C, two ways (fopen vs. open)

preview_player
Показать описание
---

This video explains two common ways to read and write files in C.

Strace/Ltrace video (might help clarify the system call/library call issue described in the video).



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Want me to review your code?

You can also find more info about code reviews here.
Рекомендации по теме
Комментарии
Автор

All this time I had been using open(), write() and read() for no good reason ahah. Looking forward to memory mapped IO video, you are really helping me get better at system programming and C.

dykens
Автор

Just wanted to add my input here. Sure, fopen() buffers its I/O, but you can disable that buffering. Secondly, as you mentioned, open is a Posix system call, but that wouldn't work on something like Windows which isn't Posix based. The Win32 API uses CreateFile() or CreateFile2() depending on how old your application is, and both of those API calls offer way more flexability than open() or fopen() does. They even support things like async Overlapped I/O. You can write to named pipes, devices, or just about anything. Even if you are exclusively targeting Posix platforms, you'd still probably use fopen() purely for the portability and just disable the buffering.

Still, it's a nice factoid that coders should know, so it's a good video.

thewelder
Автор

honestly one of the greatest programming channels

pseudopseudo
Автор

Amazing channel and videos! Very rare to find videos that talk about low level stuff. Please keep it going!

dengtianshuo
Автор

Hey, Prof. Sorber! Beau’s the name. You taught one of my CompSci classes back in 2016 or 2017. I was working on a program for my company, perusing through YouTube for some ideas on how to tackle part of it, and came across your channel. What are the chances? If I would’ve known you’d be making these videos, I could’ve saved $90, 000 🤷🏼‍♂️. In all seriousness, I hope you’re doing well. Thank you for sharing the knowledge, and good luck on your current and future endeavors. Go Tigers!

SpyderBDM
Автор

Thank you, that was a very quick and good video. Exactly what I needed to refresh my memory.

brutexx
Автор

nice job
I am continuously watching all your videos

biconomy
Автор

good information ! watch at slower speed for better understanding.

Non-residential_villager
Автор

thanks for clear explanation, still watch at 2021

WILL-zrnw
Автор

btw if you want to write a sentence into a text file it is fgets() followed by fputs()

diodedavid
Автор

Thank you for such a great explanation - really appreciate it ;)

donha
Автор

Thanks for this explanation - really clear now.

rajcodes
Автор

សួស្តី​​ Jacob, "ចេះមកពីរៀន" nice quote !!! I'm khmer student looking for tutorial on youtube, it seems hard to find good resource to learn the language like C with good explain like this, Can you do more about it ? Thanks

rithybin
Автор

Thank you very much. Very clear and helpful.

LARathbone
Автор

Could you please make a video about buffering and output in C/C++? For example if sleep() is put between calling different output functions such as printf(), puts(), etc?

konstantinrebrov
Автор

Weapprove of your new program. let's call it the "Emphaticizer." We'll pronounce the 'c' like an 's' to show our clever use and awareness of the write way to speak. Just some small discussions pertaining to 'naming' rights to get out of the way and upon receipt of the check for the 'naming;' rights, we'll show you how to use it, but most importantly, market it. win win!

rljpdx
Автор

If I'm not wrong, isn't it possible to cancel buffering for FILE structs (fopen)? Also, you can force emptying the buffer with fflush(), if I recall correctly.
What would be the difference in these cases?

vic
Автор

Would be nice to have a program/project where using MD5 or crc32 or sha1 or sha256...etc, which you use on a file with fopen, Open, fread, fwrite, and any other file access methods, to see how they treat files in different ways.
A program like hex editors, example xxd, reading the binary of the file vs reading the contents and presenting it in decimal or octal; being just more ways to play with files.

messengerofiexist
Автор

Regarding fopen calling open: Page 232 of The C Standard Library 4th edition (the edition corresponding to C89, still the most widespread version) says "...fopen must not call open....". Page 279 has the source code for "fopen.c", and it doesn't look like open() is directly called. Was this changed in later versions or is open() called indirectly further out in the call graph?

minRef
Автор

Nice little tidbit on simple "read" for binary. All the examples I come across are for text files.

Immorpher