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.
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?
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 :)
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,
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!
@@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!
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 :)
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
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!
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.
@@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
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.
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
@@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.
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.
@@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.
@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.
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.
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.
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.
Fantastic! You should have 1M subscribers by now. Great material, and great teaching skills! Keep it up!!!
Glad you liked it! And thanks for the comment, it encourages me to keep making more videos :)
I love the content. I'm gonna binge watch ❤
Glad you're enjoying the videos 😊
Thanks a lot about this video!
You're welcome 🤗
Thank you very much Henrik.
You are welcome!
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.
Yes, that's right! So you need to do more data processing if you want to remove the double quotes
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?
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 :)
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,
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!
@@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!
@iNTERnazionaleNotizia589 glad I was about to help and wishing you the best on your project!
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
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 :)
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
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!
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?
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.
@@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
@tiagobairos7357 haha nice, glad to hear and glad you were able to get it working!
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?
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.
A Tele detected! This makes the vid way more trustworthy 😊
Haha it's a strat actually. Hope that doesn't affect the vids credibility haha still fender though!
Oh gosh haha! Well, any Fender makes a vid better!😅
Haha amen!
How do you handle fields that might have commas inside the data?
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
@@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.
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.
@@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.
@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.
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.
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.
@@henrikmdev Thanks alot
I cannot reach the CSV file. Where should I put it the file in the c folder or what?
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.
music is loud...
Thanks for the input!
It is really annoying for me the background music. You don't need music for this type of videos.
Thanks for the helpful feedback!
I agree, but still good content
same..