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

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ค. 2024
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.thinkific.com
    Website ➤ www.jacobsorber.com
    ---
    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).
    • Spying on Running Prog...
    ***
    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:
    people.cs.clemson.edu/~jsorber/
    persist.cs.clemson.edu/ Note that any Amazon links in my video descriptions are generated by Amazon. If you click one of them and then buy something it helps support this channel. Thanks.
    ***
    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:
    www.jacobsorber.com
    people.cs.clemson.edu/~jsorber/
    persist.cs.clemson.edu/
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    + rep the channel with nerdy merch --- [teespring.com/stores/jacob-so...]
    Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]
    Want me to review your code?
    Email the code to js.reviews.code@gmail.com. Code should be simple and in one of the following languages: C, C++, python, java, ruby. You must be the author of the code and have rights to post it. Please include the following statement in your email: "I attest that this is my code, and I hereby give Jacob Sorber the right to use, review, post, comment on, and modify this code on his videos."
    You can also find more info about code reviews here.
    • I want to review your ...

ความคิดเห็น • 80

  • @dykens7758
    @dykens7758 6 ปีที่แล้ว +75

    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.

    • @JacobSorber
      @JacobSorber  6 ปีที่แล้ว +8

      Glad I could help.

  • @dengtianshuo
    @dengtianshuo 2 ปีที่แล้ว +3

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

  • @thewelder3538
    @thewelder3538 ปีที่แล้ว +5

    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.

  • @pseudopseudo3679
    @pseudopseudo3679 3 ปีที่แล้ว +4

    honestly one of the greatest programming channels

  • @brutexx2
    @brutexx2 2 ปีที่แล้ว +1

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

  • @donha475
    @donha475 4 ปีที่แล้ว +6

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

  • @LARathbone
    @LARathbone 4 ปีที่แล้ว +2

    Thank you very much. Very clear and helpful.

  • @WILL-zr2nw
    @WILL-zr2nw 3 ปีที่แล้ว +1

    thanks for clear explanation, still watch at 2021

  • @Non-residential_villager
    @Non-residential_villager 3 ปีที่แล้ว +1

    good information ! watch at slower speed for better understanding.

  • @biconomy
    @biconomy 4 ปีที่แล้ว +2

    nice job
    I am continuously watching all your videos

  • @ironmonkey1990
    @ironmonkey1990 3 หลายเดือนก่อน

    Thank you!

  • @rajcodes100
    @rajcodes100 4 ปีที่แล้ว +2

    Thanks for this explanation - really clear now.

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +1

      You are welcome! Glad it helped.

  • @SpyderBDM
    @SpyderBDM 2 ปีที่แล้ว +3

    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 TH-cam 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!

    • @JacobSorber
      @JacobSorber  2 ปีที่แล้ว +5

      Hi Beau. Good to see you on here. I'm doing well. I hope life after my OS class has been kind.

  • @diodedavid9229
    @diodedavid9229 3 ปีที่แล้ว +2

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

  • @vic91020
    @vic91020 4 ปีที่แล้ว +2

    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?

  • @saisrisai9649
    @saisrisai9649 ปีที่แล้ว

    Thank you!!!!

  • @thecracker1082
    @thecracker1082 3 ปีที่แล้ว

    Is it possible to have the path (filename) like an input. In my case the path to the file must be written by the user of the program. How can I do it?

  • @guyarbel2387
    @guyarbel2387 3 ปีที่แล้ว +3

    wow. such a great channel. thanks :)

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +1

      Thanks, and welcome! Glad you found it.

  • @samarthtandale9121
    @samarthtandale9121 ปีที่แล้ว

    Amazing !!! ... Just amazing 🔥

  • @gauravnimrani8936
    @gauravnimrani8936 3 ปีที่แล้ว

    i thought that after calling open() syscall , to make the process of reading writing from the files the OS maintains an open-file table so that for reading each byte or working on memory you don't search the directory retreive the fd and then start reading writing..so why is it still taking time.i mean after open.. the file is loaded into the memory (atleast some part)

  • @konstantinrebrov675
    @konstantinrebrov675 3 ปีที่แล้ว +3

    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?

    • @RobBCactive
      @RobBCactive 7 หลายเดือนก่อน

      sleep() suspends the process till the time expires or a signal occurs.
      It won't flush buffers managed by stdio.

  • @crptc5707
    @crptc5707 3 ปีที่แล้ว +1

    Nice lecture professor! Just a few questions, after open() reads a byte, how does it pass the value to user process after context switch? Secondly, does fopen maintains the buffer? I assume in this case open() would return a string or block instead of one byte, to save context switch overhead? Thanks a lot!

    • @RobBCactive
      @RobBCactive 7 หลายเดือนก่อน

      1) The kernel reads from disk or a character device into a buffer, then it uses mapped in process memory and copies to user space.
      2) The stdio library manages the buffer and provides a collection of routines that work together. The filehandle indicates the buffer.
      3) open is a system call, NOT part of stdio so if you ask for 1 char, it returns 1 or fails. If you ask for more, then you may receive less, serial terminals are by default line buffered for instance.

  • @zhouxiao3344
    @zhouxiao3344 ปีที่แล้ว

    If I called open() and fopen() with the fd from the open(). When I am done, is it OK to only call close() instead of calling fclose()?

  • @v1rusfalcon198
    @v1rusfalcon198 2 ปีที่แล้ว

    will you ever make videos on microcontrollers? like the esp32?

  • @alantao3810
    @alantao3810 3 ปีที่แล้ว

    10/10 video

  • @Sinan997
    @Sinan997 ปีที่แล้ว

    awesome

  • @bsykesbeats4013
    @bsykesbeats4013 2 ปีที่แล้ว

    I'm confused, does fopen() read the file into memory? Cuz if it doesn't, then of course you wouldn't have to fclose() to avoid leaks, but if it does, then why do I have to fread() to read it into memory if its already in memory?

  • @rithybin4129
    @rithybin4129 5 ปีที่แล้ว +4

    សួស្តី​​ 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

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      សួស្តី. I definitely plan to keep making videos on C and other topics. Let me know if you have specific requests.

    • @rithybin4129
      @rithybin4129 5 ปีที่แล้ว

      My request is to use C to solving problems, I mean practical, rather than learning the basic, thank again

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +1

      @@rithybin4129 Ok. Got it. I'll see what I can do. Thanks.

  • @rljpdx
    @rljpdx ปีที่แล้ว

    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!

  • @minRef
    @minRef 5 ปีที่แล้ว +10

    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?

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +14

      Good question. To complicate it a bit, there's "open" the system call and "open" the library function. The library function is mostly a wrapper around the system call, but in some libc implementations, it may do more. I looked quickly at a few implementations.
      opensource.apple.com/source/Libc/Libc-167/stdio.subproj/fopen.c
      github.com/freebsd/freebsd/blob/master/lib/libc/stdio/fopen.c
      www.gnu.org/software/libc/
      The apple version calls open directly. Others call an internal version (something like __open or IO_FILE_OPEN...which then calls __open). At the bottom level they're all making a low-level system call asking the kernel to open a file for them. But they don't all follow the same path to get there.
      Hope that helps.

    • @minRef
      @minRef 5 ปีที่แล้ว +3

      @@JacobSorber Thanks! That makes more sense.

    • @bonbonpony
      @bonbonpony 4 ปีที่แล้ว +2

      @@JacobSorber So much for the standards, I guess? :q

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +3

      Yeah, I guess. Standards have always been an imperfect exercise. And, this particular deviation seems unlikely to cause much harm.

    • @edwardmacnab354
      @edwardmacnab354 2 ปีที่แล้ว

      @@bonbonpony Standards tell you what should happen not how to get to what should happen because that would prevent somebody from building a better mousetrap . Take Unix, Linux , and Posix as an example .

  • @abhishekmitra5445
    @abhishekmitra5445 6 ปีที่แล้ว

    Please add more video,, We keep supporting you,, Will be grateful if you add more frequent videos

    • @JacobSorber
      @JacobSorber  6 ปีที่แล้ว +1

      Thanks! I add videos as frequently as I can. There is no shortage of topics, just a shortage of time to make videos.

    • @abhishekmitra5445
      @abhishekmitra5445 6 ปีที่แล้ว

      Yes sir as a postgraduate student of computer science I know there are ample of concepts to discuss,Sir can you please cover the topics system calls like select, poll for I/O multiplexing.
      And one thing did you have any blog sites containing lecture notes for advance concepts in CS, if yes please do share
      Thanks you for your reply

    • @JacobSorber
      @JacobSorber  6 ปีที่แล้ว +2

      I don't have any posted notes or blogs. I'll add select and poll to the list, for a future video. Thanks for the suggestion.

  • @Sparagas
    @Sparagas 2 ปีที่แล้ว

    Does anyone know what f stands in fopen?

  • @zytr0x108
    @zytr0x108 ปีที่แล้ว

    How can fopen be based on open if fopen is part of the C standard and to use open one needs to include a library?

  • @gauravnimrani8936
    @gauravnimrani8936 3 ปีที่แล้ว

    isnt buffering done by the OS itself ..for example in memory mapped file io a part of file is brought into the memory and after it is modifed then later(according to whatever policy OS follows) it is written to disk.. then why here for every byte modified file is being brought into read written and then same with next byte..in case of open() syscall

  • @dullyvampir83
    @dullyvampir83 ปีที่แล้ว +1

    Couldn't you also make open faster if you know exactly how large the file is and you completely buffer it with open? Could it maybe outperform fopen?

    • @RobBCactive
      @RobBCactive 7 หลายเดือนก่อน

      It probably won't help because of virtual memory, reusing a buffer allocates less and it will likely be hot data available in cache.
      Open is also allowed to return fewer bytes than requested so large files, will probably be fetched in chunks anyway.
      A better way to open a large structured file is mmap(2), then the VM manages the i/o and the program can treat it as in memory structures and arrays.
      Very often you only need to access parts of the file, so only reading the pages you need, rather than buffering the whole file and copying it into memory structures is far more efficient.

  • @messengerofiexist2139
    @messengerofiexist2139 3 ปีที่แล้ว +1

    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.

    • @onions8370
      @onions8370 2 ปีที่แล้ว +1

      only tangentially related, but you can use xxd to include a file directly with the -i argument

  • @bonbonpony
    @bonbonpony 4 ปีที่แล้ว +2

    Is there any way to open a file with `fopen` for writing, but only if the file doesn't exist yet? (If it exists, it should be detected and returned with error, so that the user could choose another one, instead of overwriting some important file just because its name happened to be the same :q )

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +4

      Yeah, in C11 you can use the "wx" mode with fopen and that should do what you want.

  • @Immorpher
    @Immorpher 3 ปีที่แล้ว

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

  • @sayyidalisajjadrizavi7418
    @sayyidalisajjadrizavi7418 4 ปีที่แล้ว

    Hi! I am using open("output.txt", O_WRONLY | O_CREAT) but still why do I not have permission to open it in TextEditor without sudo privileges?

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว

      I'm not sure. I would check your umask, and see what your default permissions are.

  • @GrozaRobertGenaro
    @GrozaRobertGenaro 3 ปีที่แล้ว +1

    So I have an exam, im using a mac, should I use open for more control even tho my professor will probably correct it on linux?

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +1

      If you're concerned about portability, I would probably use fopen.

  • @davidlima1525
    @davidlima1525 ปีที่แล้ว

    Hey, can u please explain what this condition checks for ? I dont really get it.
    (bytes = read(fd_to_read, &c, sizeof(c))) > 0)
    Thanks in advance!

    • @emiiiya
      @emiiiya ปีที่แล้ว

      I'm not exactly sure but it looks like the equivalent of the (c=fgetc(file_to_read)) != EOF. Simply put it checks if there's still data to be read.
      Edit:
      Checked and `read` returns amount of bytes read, so if it returns 0 it means that there is no more data to be read.

  • @Christian-qh5zu
    @Christian-qh5zu ปีที่แล้ว

    Does anyone know what vscode theme he's using? It's very nice

    • @supernovaw39
      @supernovaw39 ปีที่แล้ว

      I'm pretty sure it's Atom in this video, not VS Code

  • @ianbolfa
    @ianbolfa 2 ปีที่แล้ว

    All the cool kids use buffered I/O 😎 Great video!

  • @visalbotrchan1431
    @visalbotrchan1431 5 ปีที่แล้ว +2

    Why do you have Khmer and English Dictionary on your shelf ?

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +1

      ចេះមកពីរៀន។
      ខ្ញុំបានរោះនៅភ្នំពេញ កាលខ្ញុំនៅក្មេង
      I don't find a lot of opportunities to use Khmer these days, but I try not to lose it. ចេះខ្មែរេទ?

    • @visalbotrchan1431
      @visalbotrchan1431 5 ปีที่แล้ว +1

      បាទបង។ ភាសារខែ្មរជាភាសារកំណើតរបស់ខ្ញុំ ហើយ ខ្ញុំក៏ធ្លាប់បានរស់នៅទីក្រុងភ្នំពេញដែរ។
      Currently, I am taking a system programming class in C and find your videos very informative.
      Are there many Cambodian students in your college? There aren't any at my college.

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +1

      I haven't met any Khmer students here, but it's a big student body. So, maybe I just haven't met them yet. Glad you enjoyed the videos.

  • @fitradical
    @fitradical 3 ปีที่แล้ว

    THANKS SO F* MUCH

  • @26487913
    @26487913 2 ปีที่แล้ว

    0:27 there is one file you cannot keep straight, the homofile :'D

  • @theomiddehghan
    @theomiddehghan 3 ปีที่แล้ว +1

    The tutorial, very good!
    Dress shirt, no!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      Are you opposed to dress shirts in general, or just the sad fit of the one in this video? 🤔

    • @theomiddehghan
      @theomiddehghan 3 ปีที่แล้ว

      ​@@JacobSorber Oh no I'm OK with this type of shirt (even though don't remember the last time I wear one!). You bring energy and excitement to your content but wearing a dress-shirt I guess somehow doesn't fit in the context very well! But the T-shirt fits perfectly:)

  • @Awwe12675
    @Awwe12675 2 ปีที่แล้ว

    Open faster than fopen

  • @xbz24
    @xbz24 ปีที่แล้ว

    Back when atom was a thing

  • @alacastersoi8265
    @alacastersoi8265 3 ปีที่แล้ว

    fNAME = formatted NAMES