I would go about it something like this:
read in a line of data
for i in number of colums
do
extract column i
switch on i
i == 1 => convert column-as-string to type 1
i == 2 => convert column-as-strint to type 2
... etc ...
There's a basic outline of how you might go about this. You'll note that I've not given you any actual code. This is your homework, and you'll probably need to build on what you learn doing this as you progress through your course, so it's important that you do the work yourself.
Some things to think about:
Can you process each line of data as its read, and then throw the data away, or do you need to keep it?
If you need to keep it, how do you think you might do that? Perhaps you could design a
struct
to represent a single line of data? Then maybe you could have an array of structs to represent the whole table. Then you need to think about how to make the array the right size. You might need to use malloc and maybe realloc to dynamically size the array.
What do you know about the data? Will fields have embedded commas? If so, how will they be denoted? Will they be "escaped" e.g.
able\,baker
or will the field be enclosed in quotes e.g.
"able,baker"
. Then you'll have to think about fields like
"able baker"
. Are the quote marks significant or not?
As you say you're a beginner, so I don't think the course will expect you to handle most of the tricky things that a CSV file can throw at you. But everything you need to solve this assignment should have been covered in the course. Think about what I've said, and the basic outline for processing a CSV, re-read your course material and notes, and see what you can come up with.