How To Read a CSV File in C
Вставка
- Опубліковано 11 лют 2025
- Learning how to program?
From no coding experience to building your first app
📘Download my FREE programming guide here: henrikmdev.com...
Need 1-on-1 coaching?
💻Click below to learn more about my mentoring program:
henrikmdev.com...
One of the coolest things you can do through programming is to process data from a CSV file. Once you know how to do this, you can get data from the internet or from other programs and write a program or application that processes that data for you. For example, in my programming guide, you will need to learn how to do this to read and process data from the transactions file that your credit card company can give you.
Here are some examples of when parsing a CSV comes in handy:
-To automate something in your business
-To read a file and do all the mundane calculations for you
-To give configuration information to your application
-To save and load user data
-To analyze data
This tutorial shows you the functions that you need to open, read, and close a file on your computer. It is one of the most useful things you can get your program to do especially if you're just learning how to program. It gives you a lot of functionality and will open up many kinds of ideas for applications.
Reading and parsing a CSV file is a fundamental skill every programmer should know how to do. Let's learn!
In this video, you'll learn:
✅What a CSV file is
✅How to open and close a it in C
✅How to parse it in C
🎥 Visual Studio Code Tutorial:
• How To Start Coding fo...
=================================================
📚 Stay Tuned for More:
If you found this video helpful, make sure to like and subscribe to our channel for more programming tutorials and tips. We have a lot more exciting content in store for you, so stay tuned!
/ @henrikmdev
👨💻 Have Questions?
If you have any questions or want to suggest topics for future videos, please leave a comment below. We love hearing from our viewers and are here to help!
🔔 Turn on Notifications:
Don't forget to ring the notification bell so you never miss an update from us. Stay ahead in your programming journey!
=================================================
💡 Coding Tutorials:
• Coding tutorials
💡 Dev Tool Tutorials:
• Dev tools
📁 Sample Code:
Find the sample code used in our tutorials on our GitHub page:
github.com/hen...
=================================================
📚 Books
C Programming Language: amzn.to/4etzNE5
Learning the bash Shell: amzn.to/483dvGN
⚙️ Gear
Webcam - Logitec Brio: amzn.to/3zUqfTG
Lighting - Ring Light Clip: amzn.to/3Y23yoj
Microphone - AKG Pro Audio P220: amzn.to/40hunIf
Audio Interface - Focusrite Scarlett 2i2: amzn.to/4gVTpSX
Thank you for watching, and happy coding! 💻🧡
-Henrik
Disclaimer: Some of these links are referral links. I may earn a commission if you use them, at no extra cost to you. You're not obligated to use these links, but it would be appreciated. Thanks!
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 😊
Thank you very much Henrik.
You are welcome!
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!
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!
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!
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
Thanks a lot about this video!
You're 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 :)
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 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 :)
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!
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..