Read And Write An Array Of Structs To A Binary File | C Programming Example

แชร์
ฝัง

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

  • @gabrielfairbairn9404
    @gabrielfairbairn9404 ปีที่แล้ว +9

    Your videos have been saving my academic life! C is very hard to understand but I find it so easy to sit through your whole videos and always come out with a better understanding, even after learning the same thing in a classroom. The examples really transfer over well to assignments I've been getting. They require some adjustment of course but that hasn't been a problem since I actually understand the code I'm writing now. Thank you!

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

      You're welcome Gabriel! :-) And thank you so much for taking the time to share that with me. Helping out students like you is *exactly* what motivates me to make these videos, so I'm really glad to hear they have helped you out!

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

    Thank you so much for this video, it really helped me out in understanding how binary i/o works in C. In my personal project, I computed the gravitational interactions between n particles, which can take quite a while. Thus I wanted the ability to write the data to a binary and then later read it. This was the first tutorial I've found in a long time that has eloquently explained a topic, so seriously, thank you for this!

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

      Thank you so much for taking the time to write this kind feedback, it makes me really happy to hear that a video has helped somebody out. :-).

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

    Excellent job, Kevin. I can see many uses to this Struct allocation in embedded design. Thank you very much.

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

    Really nicely paced example code which was logical and didn't get bogged down with irrelevant guff. 👍

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

      Thank you very much for the positive feedback, I'm glad you enjoyed the video! :-)

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

    This was amazing, this video helped me to solve all the issues in my code.

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

      That's awesome Lucas I'm so glad to hear the video helped you out! :-)

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

    This was teached really verry clear. Thank you for teaching so nice and clear. 👍

  • @CarlosDaniel-od7bx
    @CarlosDaniel-od7bx ปีที่แล้ว

    Thank you!

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

    or anything after or anytime and thanks for seeing these

  • @zaman-sheikh
    @zaman-sheikh ปีที่แล้ว

    Quality 👌👌

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

    Okay so this works! Thanks. Though instead of char array when I use char * in Student structure, this doesn't work? I need a solution for that. How to make it work when u have a char * in your structure instead of char array?

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

    Hey man,
    Can you please make a video on what C++ topics we should know to be able to master C++. I mean yes, it's a life-long journey and we have to keep learning, but CS students like me and other self-taught programmers might not feel comfortable applying to jobs with their current abilities. Impostor syndrom plays a big role in this, but also not knowing how much topics we should know also make us uncomfortable to apply for SW jobs.

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

      That's an interesting idea for a video. I may try to do something like that one day, perhaps going over recommendations for what is important to know in a language. The only problem is that there's a *lot* of stuff that's important so it could be a long video. :-)
      In general one thing that's very important in "real-world applications" is accessing a database. Many applications rely on a database, and a lot of the code will be related to accessing and modifying the data in that database. That's something that I find a lot of courses and textbooks don't cover very much.

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

      @@PortfolioCourses Thank you so much for that man! That answer alone has already helped me. My professors never touched anything about databases (at least not yet). And I think I'll learn it myself at this time :)

  • @mohamedgendia4274
    @mohamedgendia4274 6 หลายเดือนก่อน

    In the read file you almost deal with every assumed error and free the memory . But something i do not understand at the last end of the function we returned pointer to the allocated memory address and we did not free it after i do understand that if we free it that means our return pointer will not be valid no more but is that mean everytime we call this read file it will allocated new memory ? And is not freed up? Is that not memory leak ?
    I am sorry if my question is looked stupid but i am trying to learn more in how the memory behave in those situations

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

    Which part of the write and read function do the writing and reading? Are they executed despite being within an if-statement?

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

      When we call fwrite inside the write_data() function that does the writing, and when we call fread() inside the read_data() function that does the reading. The call to write_data() is an if-statement condition, not in the if-statement body, so that means it will execute no matter what. The write_data() function will return true/false based on whether it was successful or whether there was an error, that's why we have it inside an if-statement condition, so that if it does fail we can exit with an error message and status. :-)

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

    Why on the read mode, you didnt free its allocated memory after return it ? Can we do it before return ? Im pretty sure the all the data will gone, right ? :’) is there any solution to free it ?

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

    Great. But there is a need to fclose(file) even if errors happens. Then, before return false, or return NULL in the functions, we must call fclose() or, better, creating a single point of failure and goto there if an error and a single point of success if no errors.

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

    Hey man, I love your videos, I don't know if you'll ever reply but when I compiled the code of *read_data an warning appeared in my console which says: "warning: comparison between pointer and integer" any possible fix to that? Thx

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

      Thank you Juani! Just out of curiosity which line is giving you this warning? And what compiler are you using? I did not get the warning when using gcc.

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

      @@PortfolioCourses if ( fread ( data, sizeof ( student ), *total, file != total )){} That's the line, maybe I should cast it? I'm using gcc too, but in a windows pc.

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

      @@juanivillanueva7873 Oh there needs to be a * in front of total, like there is in the video. Otherwise total will be a pointer and not an int value, and that warning will occur. The code is posted in the video description in the github link. :-)

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

      @@PortfolioCourses Thank you, God bless

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

      You’re welcome! :-)

  • @marbles5590
    @marbles5590 8 หลายเดือนก่อน

    in the line: if(fwrite(data, sizeof(Student), total, fp)!=total) return false;
    it says: comparison between signed and unsigned integer expressions.
    Program is working but i just wanna eradicate this warning in vscode

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

    please do the same for C++ using classes ... link ?

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

      I don't have a video on that topic, but that's a good idea. :-) I would need to assume that member variables have getters/setters or can somehow otherwise be accessed, though that's probably an OK assumption if we're trying to read/write arrays of the objects to files.

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

    whatever anything I do or know or knew or anything I did not know