Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Example of csv file:

3,4
,,C 200,
M Healing Potion:85,,M Defense:360
,,,

The first row of the file is the number of row and column, second row onwards are the data that I have to put into a 2d array. Each comma separator is a differenent column.

I am struggling to think of a way to read the data, especially with commas in between the words

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

/*I know I have to use structure, but I am not sure how to start*/
typedef struct{

}

int main(int argc, char *argv[])
{
FILE *map;

/*I have done declaring a 2d array with malloc*/

}
Posted
Updated 1-May-19 5:09am
v2

That's not really a CSV file - it's a set of lines of data which contain items separated by commas: a "true" CVS file contains an optional header line, then rows which contain the same number of items of the same type in each column, optionally using double quotes to delimit string values.

There's a big difference here: CSV is a table, not a "random" collection of data.

You data looks organised into rows, but the columns aren't a table - so don't call it a CSV file, call it a "configuration file" or similar because that better reflects the structure of your data. Not every row in your sample data contains four items (count the commas on the "healing potion" line for example).

Start by reading each line - which you should know how to do - and then dividing each into sections by finding the commas.

I wouldn't leap right into a structure - it's not that obvious what data types you will need from that little an example - but a 2D array is pretty trivial: read the first line and it gives you the sizes, so you can then construct a 2D array of char* . Then each separate item you find up to a comma gets copied to a new char buffer, null terminated, and set to the appropriate 2D array element.

Try it on paper and you should see what I mean.
 
Share this answer
 
Comments
OriginalGriff 1-May-19 6:36am    
Start by taking a single line, and write a function that returns a pointer to an array of char* values that breaks it down into separate strings - that's pretty easy, just remember to use malloc for the whole array, and then again for each individual string as you break them up. (Hint: strcpy could be useful here!)

Test it, and make sure it works.
Member 14351155 1-May-19 6:34am    
Could you please write me a short example? I am still not very sure how to do that.
I have only written the declaration of a 2D array:

char **arr;

arr = (char**)malloc(row*sizeof(char*));
for(I=0; I
Member 14351155 1-May-19 6:43am    
So sorry, its difficult for me to understand by words, can I have an example?
OriginalGriff 1-May-19 6:55am    
No, come on - this isn't difficult and you will learn nothing if I just give you a solution.
Write a "framework" for the method: return an array of char* pointers, accept a single char* parameter.
When you have that, show me.
Member 14351155 1-May-19 8:18am    
What is mean by this sentence "Then each separate item you find up to a comma gets copied to a new char buffer, null terminated, and set to the appropriate 2D array element."?
char **arr;

arr = (char**)malloc(row*sizeof(char*));

for(i=0; i<row; i++)
{
arr[i] = (char*)malloc(column*sizeof(char));
}


for(i=0; i<row; i++)
for(j=0; j<column; j++)
 
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