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.
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!
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 :)
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 :)
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.
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.
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?
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 :)
Thanks a lot about this video!
You're welcome 🤗
I love the content. I'm gonna binge watch ❤
Glad you're enjoying the videos 😊
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
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!
Thank you very much Henrik.
You are welcome!
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 :)
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 :)
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
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.
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.
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.
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!
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..