Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have a little problem.
I have a mandatory homework in C, which consists of processing the data from a CSV file.
The problem is the following, we are unable to import the data from the CSV file in the form of a one-dimensional table, the document presents data of types int and char and in very large quantities (a few thousand).
Thank you in advance for your help !
In my opinion a more advanced developer than me will find this operations very easy ^^ but I am still a beginner

What I have tried:

Quote:

Not much Functional
Posted
Updated 5-Jan-22 2:53am
Comments
Patrice T 4-Jan-22 13:21pm    
Show your work so far and explain how it fail.
0x01AA 4-Jan-22 13:36pm    
Quote: '... a more advanced developer than me will find this operations very easy... '
Parsing a CSV (in all the details and especally in C) is not that easy ;)

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.
 
Share this answer
 
v2
Comments
0x01AA 4-Jan-22 13:53pm    
+5
Typically one imports data from a CSV file as a 2D table. Each line of data represents one row of data in the table and the data in each row is delimited by commas which separate the columns of the table.

CSV file parsing can be very tricky when a quoted string can have commas embedded in them as k5054 mentioned but I seriously doubt you need to worry about that. For simple CSV file parsing I have used the function strtok successfully many times. I recommend that you start by having a look at it : strtok - C++ Reference[^]

I would read the file line-by-line with fgets - C++ Reference[^] and call strtok to separate the tokens.
 
Share this answer
 
Comments
Victor Gassmann 5-Jan-22 9:52am    
Hello, thanks for your answer, it's really helpfull. Yes the work is for a Homework Project. We can use the strtok and fgets or getline to read the CSV file, however we have some problem with it, because it's totally knew for us and we don't know how to use it.
Create a structure containing a data member for each field identified in the CSV.

Create a method in your program that will handle reading the CSV file

Create a file object, which will read in the data, using the following code: FILE * pInput;

Create a character buffer large enough to hold one line of the file at a time.

Open the file Ex:fileopen("filename","r");

Read in a line of the file using the following code:

C++
fgets(buf, sizeof(buf), pInput)

Parse the CSV using the function "strtok". Create a new character string to point to the tokens, and initialize it with data from the line read in above: char *tok = strtok(buf, ",")

Convert the received token into the appropriate data. Using the example line: 1, "test", 3.45 convert the data contained in "tok" to an integer using the following code: row.col1 = atoi(tok);

For subsequent reads from the same line you can pass "strtok" a NULL parameter

Then, convert the token to the appropriate data type.

Do this for all of the entries on each line of the CSV. The function "strtok" will continue to provide the data between comma values until it runs out of data in the buffer, at which point it will return NULL. This will indicate that you have finished with the line.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900