我的問題是這樣的:我必須讀取包含一些字符串的文件。任務是讀取數據並將其存儲在C程序的適當數據結構中。可以打印但不能將字符串存儲到數組中C
目前我的程序打印出所有值,但訪問這些變量是一個問題...
using namespace std;
int split(char* str, char splitstr[15][10]);
int main()
{
FILE *fp;
char str[20] = {0}; // temp variable for accessing a line from file
// for opening of file
fp = fopen("C:\\Cross Crystal Sheet.csv", "r") ;
char input[256];
char result[15][10];
char *protein[700];
char p[1000];
int j=0;
if (NULL != fp)
{
while(fgets(str,sizeof(str),fp)!=NULL)
{
strcpy(input, str);
int count = split(input, result);
int tmp=count;
//j=result[0]-'0';
for (int i=0; i<count; i++)
{
printf("%s\n", result[i]);
//printf("%s\n",*(result+i));
protein[j]=*(result+i);
//*((protein)+j);
printf("%s \n",*(protein+j));
j++;
}
}
}
}
int split(char* str, char splitstr[15][10])
{
char* p;
int i=0;
char *string = strdup(str);
p = strtok (string, ",");
// i=i+count;
while(p!=NULL)
{
strcpy(splitstr[i++], p);
p = strtok (NULL, ",");
if(p ==NULL)
{
break;
}
unsigned charlength = strlen(p);
if(charlength==1 ||charlength==2)
{
break;
}
}
return i;
}
我期待輸出這樣 蛋白質[] = {1,ABL1,ABL2,AURKA,AURKB,。 ..}
數據文件是這樣的:
1,ABL1,ABL2,,,,
,,AURKA,,,,
,,AURKB,,,,
,,BMX,,,,
,,BTK,,,,
,,KIT,,,,
,,LCK,,,,
,,MAPK14,,,,
,,PRKACA,,,,
,,SYK,,,,
,,EGFR,,,,
,,INSR,,,,
,,MAPK11,,,,
,,,,,,
2,ABL2,ABL1,,,,
,,AURKA,,,,
,,AURKB,,,,
,,CAMK4,,,,
,,CDKL2,,,,
,,CLK3,,,,
,,CSNK1G3,,,,
,,KIT,,,,
,,LCK,,,,
,,MAPK14,,,,
,,PRKACA,,,,
,,SLK,,,,
,,SYK,,,,
,,,,,,
3,ACVR1,ACVR2A,,,,
,,ACVRL1,,,,
,,PIM1,,,,
,,PRKAA2,,,,
,,,,,,
4,ACVR2A,ACVR1,,,,
,,CAMK2D,,,,
,,MST4,,,,
,,PRKAA2,,,,
,,SLK,,,,
,,,,,,
5,AKT1,PRKACA,,,,
,,,,,,
,,,,,,
6,ALK,FES,,,,
,,MET,,,,
,,,,,,
7,AURKA,ABL1,,,,
,,ABL2,,,,
,,AURKB,,,,
,,CDK2,,,,
,,CHEK1,,,,
,,PLK1,,,,
,,PRKACA,,,,
,,,,,,
8,AURKB,ABL1,,,,
,,ABL2,,,,
,,AURKA,,,,
,,PRKACA,,,,
,,,,,,
9,BMX,ABL1,,,,
,,BTK,,,,
,,LCK,,,,
,,MAPK14,,,,
,,,,,,
10,BRAF,CDK8,,,,
,,KDR/VEGFR2,,,,
,,MAPK14,,,,
,,RAF,,,,
,,,,,,
這不是C.你的意思是C++或C#也許? – pmg
您可以編輯您的問題以包含實際輸出(但不包括所有輸入文件)以及預期輸出。 –
你必須檢查陣列尺寸 – Riskhan