Reputation: 216
I want to read a file with mixture of strings and numerals and store it into 2d array. The condition is the first row and first column should act as indices to my 2d array. Here is the example file.
xx,yy
aaa,10,11
bbb,12,13
ccc,14,15
ddd,16,17
eee,18,19
To be clear, How can I give strings as indices and my array should give me values like this
array[aaa][xx] = 10
array[bbb][yy] = 13... etc.,
Here is my approach
FILE* fp1 = fopen("test.csv","r");
if(fp1 == NULL)
{
printf("Failed to open file\n");
return 1;
}
char s[1] = ",";
fscanf(fp1,"%[^\n]",array); // Read first row alone
token = strtok(array,s);
while( token != NULL )
{
strArray[i] = strdup(token); // First row stored in strArray[i]
strcpy(strArray[i], token);
token = strtok(NULL, s);
i++;
}
i=0;
while((fscanf(fp2,"%[^,],%[^,],%[^\n]\n",Col1,Col2,Col3)>0)) // Reading File Column wise
{
Column_1[i][j]= strdup(Col1);
Column_2[j]= atof(Col2);
Column_3[j]=atof(Col3);
j=j+1;
}
for(i=0;i<1;i++)
{
for(j=0;j<=5;j++)
{
myArray[i][j] = Column_2[j];
}
}
for(i=1;i<2;i++)
{
for(j=0;j<=5;j++)
{
myArray[i][j] = Column_3[j]; // Column 2 & 3 values stored in myArray[i][j]
}
}
Now search for the Required String in first column and first row and get the indices and search in myArray[i][j] with that indices to get the values. It is working absolutely fine, but I felt I had made code a bit complex. I am looking for simpler code as possible. Can someone suggest please.
Thanks in Advance, Siva
Upvotes: 1
Views: 198
Reputation: 290
A method I used today was:
string[][] values = new string[2][];//string = open value|new string = fill value
int i = 0;
while (!sr.EndOfStream)
{
strline = sr.ReadLine();
values[i] = strline.Split(',');//'i' is row 1
i++;// this is row too
} };
}
sr.Close(); // Release the file.
Console.WriteLine(values[0][0]);//test your code with this value for row1 and column1
Upvotes: 0
Reputation: 216
FILE* fp1 = fopen("test.csv","r");
if(fp1 == NULL)
{
printf("Failed to open file\n");
return 1;
}
char s[1] = ",";
fscanf(fp1,"%[^\n]",array); // Read first row alone
token = strtok(array,s);
while( token != NULL )
{
strArray[i] = strdup(token); // First row stored in strArray[i]
strcpy(strArray[i], token);
token = strtok(NULL, s);
i++;
}
i=0;
while((fscanf(fp2,"%[^,],%[^,],%[^\n]\n",Col1,Col2,Col3)>0)) // Reading File Column wise
{
Column_1[i][j]= strdup(Col1);
Column_2[j]= atof(Col2);
Column_3[j]=atof(Col3);
j=j+1;
}
for(i=0;i<1;i++)
{
for(j=0;j<=5;j++)
{
myArray[i][j] = Column_2[j];
}
}
for(i=1;i<2;i++)
{
for(j=0;j<=5;j++)
{
myArray[i][j] = Column_3[j]; // Column 2 & 3 values stored in myArray[i][j]
}
}
printf("\n\nEnter Row_name to search\t");
scanf("%s", Row_name);
printf("\nEnter Column_name to search\t");
scanf("%s", Column_name);
for(i=0;i<1;i++)
{
for(j=0;j<=5;j++)
{
if(strcmp(Row_name,Column_1[i][j]) == 0)
{
i_index = j;
}
}
}
for(i=0;i<3;i++)
{
if(strcmp(Column_name,strArray[i]) == 0)
{
j_index = i;
}
}
printf("\nValue[%s][%s] is '%f'",Row_name,Column_name,myArray[j_index-1][i_index] );
fclose(fp1);
return 0;
}
Upvotes: 0
Reputation: 374
With the limited knowledge I have I really don't think you can declare a 2D array of type char
and take int
values. Instead I would suggest to go for structures.
Here's an inlink link to structures :: (http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Structures)
The other naive solution is you statically declare the first row and first column, take the input as a character and convert it to integer via the atoi()
function.
Upvotes: 1