How To Read a CSV File in C

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

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

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

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

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

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

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

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

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

      Glad you're enjoying the videos 😊

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

    Thanks a lot about this video!

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

      You're welcome 🤗

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

    Thank you very much Henrik.

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

      You are welcome!

  • @soniablanche5672
    @soniablanche5672 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน

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

  • @rakol96
    @rakol96 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน

      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 :)

  • @iNTERnazionaleNotizia589
    @iNTERnazionaleNotizia589 26 วันที่ผ่านมา +1

    Hi I have question, is it possible for the 'While loop fgets(buffer))' to be implemented in OpenMP?
    I want to read from a CSV file, 25000 rows, and I want to parallelize that with openmp
    Please give me advice on how to do that (or source)
    Best,

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

      By design using a while loop with fgets is sequential so each line must be read in order. One idea is you could split the file into separate files and sequentially read in the files through different threads. Hope you can get it to work!

    • @iNTERnazionaleNotizia589
      @iNTERnazionaleNotizia589 25 วันที่ผ่านมา +1

      @@henrikmdev That also came into my mind! but we have a research project for investigating on how to read 1 file using openmp multithreading
      but thanks for the insight from the expert like you! we now have the idea/concept of the limit of our project
      Much appreciated!

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

      @iNTERnazionaleNotizia589 glad I was about to help and wishing you the best on your project!

  • @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 :)

  • @alwaysjdp
    @alwaysjdp 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน

      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!

  • @tiagobairos7357
    @tiagobairos7357 13 วันที่ผ่านมา +1

    not sure if it is too late to comment: but how would you go about storing the data as an integer, as strtok saves it as a character pointer?

    • @henrikmdev
      @henrikmdev  13 วันที่ผ่านมา +1

      Not too late to comment :) in general after using strok, you could convert it to an integer using library functions like atoi or sscanf. If you don't want to use strtok, you could use something like sscanf, but you lose that tokenizing ability that comes with strtok.

    • @tiagobairos7357
      @tiagobairos7357 13 วันที่ผ่านมา +1

      @@henrikmdevI was not expecting such a fast response, I eventually figured out atoi would work. But thank you anyway, the video was perfect for learning everything I needed, you have saved me for my university coursework haha

    • @henrikmdev
      @henrikmdev  13 วันที่ผ่านมา +1

      @tiagobairos7357 haha nice, glad to hear and glad you were able to get it working!

  • @lemoniecake123
    @lemoniecake123 8 หลายเดือนก่อน +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  8 หลายเดือนก่อน

      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.

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

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

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

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

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

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

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

      Haha amen!

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

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

    • @henrikmdev
      @henrikmdev  11 หลายเดือนก่อน +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 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน +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 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน +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.

  • @LeThao090
    @LeThao090 4 หลายเดือนก่อน +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  4 หลายเดือนก่อน +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 4 หลายเดือนก่อน +1

      @@henrikmdev Thanks alot

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

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

    • @henrikmdev
      @henrikmdev  ปีที่แล้ว +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.

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

    music is loud...

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

      Thanks for the input!

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

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

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

      Thanks for the helpful feedback!

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

      I agree, but still good content

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

      same..