S
Samier
hi,
im trying to asign the value of a csv file to a two dimensional array.
The csv file has multiple lines and each line in the file gets its own
line in the array. Now I did read a some posts on that topic in this
group and came up with the following idea for a function
------
int read_in_file (FILE *csvfile, char *b[MAXI][MAXI] )
{
char zeile[MAXI] ;
int i = 0, j = 1 ;
while (fgets(zeile, MAXI, csvfile) != NULL)
{
b[0] = strtok (zeile, ",") ;
while ( (b[j] = strtok (NULL, ",")) != NULL )
{
printf ("b[%d][%d] = %s ", i, j, b[j]) ;
j++ ;
}
printf("\n\n") ;
j = 1 ;
i++ ;
printf ("\n In function -->\t%s\n", b[1][1]) ;
return 0 ;
}
---
Actually this works for every line itself just fine. But as soon as a
second line is read the old information is lost and presumably
overwritten by the new line. This is shown by last printf statement
for every line greater than 1.
Before I try a solution with malloc to make the information somehow
permanent I would like to know what this group thinks would be a
better solution.
Thanks in advance
sake.
im trying to asign the value of a csv file to a two dimensional array.
The csv file has multiple lines and each line in the file gets its own
line in the array. Now I did read a some posts on that topic in this
group and came up with the following idea for a function
------
int read_in_file (FILE *csvfile, char *b[MAXI][MAXI] )
{
char zeile[MAXI] ;
int i = 0, j = 1 ;
while (fgets(zeile, MAXI, csvfile) != NULL)
{
b[0] = strtok (zeile, ",") ;
while ( (b[j] = strtok (NULL, ",")) != NULL )
{
printf ("b[%d][%d] = %s ", i, j, b[j]) ;
j++ ;
}
printf("\n\n") ;
j = 1 ;
i++ ;
printf ("\n In function -->\t%s\n", b[1][1]) ;
return 0 ;
}
---
Actually this works for every line itself just fine. But as soon as a
second line is read the old information is lost and presumably
overwritten by the new line. This is shown by last printf statement
for every line greater than 1.
Before I try a solution with malloc to make the information somehow
permanent I would like to know what this group thinks would be a
better solution.
Thanks in advance
sake.