How To Read a CSV File in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ต.ค. 2024

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

  • @diegogarridomendoza2828
    @diegogarridomendoza2828 9 หลายเดือนก่อน +6

    Fantastic! You should have 1M subscribers by now. Great material, and great teaching skills! Keep it up!!!

    • @henrikmdev
      @henrikmdev  9 หลายเดือนก่อน +1

      Glad you liked it! And thanks for the comment, it encourages me to keep making more videos :)

  • @michellemorais3324
    @michellemorais3324 11 วันที่ผ่านมา +1

    Thanks a lot about this video!

    • @henrikmdev
      @henrikmdev  11 วันที่ผ่านมา

      You're welcome 🤗

  • @BXBanda
    @BXBanda 10 หลายเดือนก่อน +2

    I love the content. I'm gonna binge watch ❤

    • @henrikmdev
      @henrikmdev  10 หลายเดือนก่อน +1

      Glad you're enjoying the videos 😊

  • @soniablanche5672
    @soniablanche5672 9 หลายเดือนก่อน +2

    the problem with strtok is that it will take delimiter that are inside double quotes, I think CSV treats delimiters inside of quotes as part of the data.

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

      Yes, that's right! So you need to do more data processing if you want to remove the double quotes

  • @alwaysjdp
    @alwaysjdp 9 หลายเดือนก่อน +1

    Thank you for the help, but I still with two questions...what if only want the Age and the Weight?How can I assign the data into an array of strucks?Thanks

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

      If you only want the age and weight, just use strtok again to skip the fields you don't care about and move on to the next field in the file.
      For your second question, if you have an array of structs, you can just assign the fields you want in your struct to the strings you parsed when you get them.
      Hope that helps!

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

    Thank you very much Henrik.

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

      You are welcome!

  • @rakol96
    @rakol96 10 หลายเดือนก่อน +1

    Simple and pleasure to watch, thx. What if there is no single token in line? In example: "token1",,"token3"?
    Also, could I do the same functions in AVR GCC for microcontroller like ATmega, ATxmega?

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

      If there are consecutive delimiters (aka commas), then strtok would just treat it as one delimiter. The strtok function is in the C library string.h. You will need to check your microcontrollers documentation to see if they support that library. Or you can just try including that library and see if it compiles :)

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

    I downloaded your programming guide but its very helpful. Meanwhile what libraries? I usually here react library etc but I don't know what it means

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

      You don't have to worry about react libraries right now. In C, the are some standard libraries that you can use. Just do "#include" the way I showed in the video and that's all you need to do to use that library :)

  • @LeThao090
    @LeThao090 3 หลายเดือนก่อน +1

    I want to ask if we don't have data of age, when we print the data of age is data of height. IS there any way to print NULL instead data of height. Oh, thanks for the video, it helps me alot.

    • @henrikmdev
      @henrikmdev  3 หลายเดือนก่อน +1

      Glad it was helpful! Yes, you can print NULL instead of height. You just need the program to know when the line doesn't have age data. If it doesn't have age data, put NULL instead of reading in the value that corresponds to height.

    • @LeThao090
      @LeThao090 2 หลายเดือนก่อน +1

      @@henrikmdev Thanks alot

  • @SlideRSB
    @SlideRSB 9 หลายเดือนก่อน +1

    How do you handle fields that might have commas inside the data?

    • @henrikmdev
      @henrikmdev  9 หลายเดือนก่อน +1

      Good question, you could just read in both fields and then concatenate them back together and store them in the string variable that you want to store them in

    • @SlideRSB
      @SlideRSB 9 หลายเดือนก่อน +1

      @@henrikmdev I would imagine you would need to test that the token is not incomplete as the data may or may not include the delimiters within the field. I guess we can check the token for the presence of the starting *and* ending double quote.

    • @henrikmdev
      @henrikmdev  9 หลายเดือนก่อน +1

      Yeah, that's another way of doing it. Another approach is if you know that each field will be in double quotes, you could also try tokenizing based on the double quotes instead of the commas.

    • @SlideRSB
      @SlideRSB 9 หลายเดือนก่อน +1

      @@henrikmdev That's probably a better approach. This seems like difficult balancing act since you can't always predict what form the data will take. I think Excel produces CSV files with character strings encapsulated in double quotes and numbers not in quotes.

    • @henrikmdev
      @henrikmdev  9 หลายเดือนก่อน +1

      @SlideRSB yeah, you just need to remember to handle all the necessary cases. This is where carefully coming up with test csv files come in handy to make sure you're always supporting all the different formats.

  • @lemoniecake123
    @lemoniecake123 7 หลายเดือนก่อน +1

    So I am trying to read a csv file and get the data from a specific column in each line. How would I go about doing that?

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

      Yeah, something simple you can do is just read all the data into your program. And then just use the data from the columns you care about. Alternatively, you can just also call strtok for the columns you don't care about, but don't store it into a variable. Just use strtok to continue reading the file.

  •  11 หลายเดือนก่อน +1

    I cannot reach the CSV file. Where should I put it the file in the c folder or what?

    • @henrikmdev
      @henrikmdev  11 หลายเดือนก่อน +1

      You can specify the path like this:
      fopen("C:\path\to\file.txt", "r")
      This is called using an "absolute path". But if you just want to use the filename, then yes it has to be in the same directory as your C file.

  • @handlewithoutsuitcase
    @handlewithoutsuitcase 7 หลายเดือนก่อน +1

    A Tele detected! This makes the vid way more trustworthy 😊

    • @henrikmdev
      @henrikmdev  7 หลายเดือนก่อน +1

      Haha it's a strat actually. Hope that doesn't affect the vids credibility haha still fender though!

    • @handlewithoutsuitcase
      @handlewithoutsuitcase 7 หลายเดือนก่อน +1

      Oh gosh haha! Well, any Fender makes a vid better!😅

    • @henrikmdev
      @henrikmdev  7 หลายเดือนก่อน +1

      Haha amen!

  • @Enzzo777
    @Enzzo777 3 หลายเดือนก่อน +1

    music is loud...

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

      Thanks for the input!

  • @user-ur2bx2yx4r
    @user-ur2bx2yx4r 9 หลายเดือนก่อน +1

    It is really annoying for me the background music. You don't need music for this type of videos.

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

      Thanks for the helpful feedback!

    • @lightspd714
      @lightspd714 8 หลายเดือนก่อน +1

      I agree, but still good content

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

      same..