#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int mystrtok(char *str);
int main()
{
FILE *fp = fopen("t2.txt","r"); //读取配置文件
char ch[1024];
char ch1[1024];
int row[1024];
char ***file;
char *pp;
int count = 0,count1 = 0,i = 0;
while(fgets(ch,1024,fp))
count++;
file = (char***)malloc(sizeof(char**)*count);//为三级指针分配空间。
rewind(fp);
while(fgets(ch,1024,fp))
{
strcpy(ch1,ch);// 拷贝一份,避免ch里面的内容被修改
count1 = mystrtok(ch1);//每一行被分割的段数。
row[i] = count1;// 将每一次被分割段数保存起来, 便于三级指针的访问。
char **p = (char**)malloc(sizeof(char*)*count1);
for(int j = 0; j<count1; j++){
if(j == 0){
pp = strtok(ch,"] \n");//分割ch以"] \n"(左中括号,空格,换行)为标准。
p[j] = (char*)malloc(sizeof(char)*strlen(pp)+1);
strcpy(p[j],pp);
}
else{
pp = strtok(NULL,"] \n");
p[j] = (char *)malloc(sizeof(char*)*strlen(pp)+1);
strcpy(p[j],pp);
}
}//for循环一次得到一个二维数组。数组的地址是p;
file[i++] = p;
}
for(i = 0; i<count; i++)
for(int j = 0; j<row[i]; j++)
printf("%s\n",file[i][j]);
return 0;
}
int mystrtok(char * str)
{
char *p = str;
int count = 1;
p = strtok(p,"] \n");
while(strtok(NULL,"] \n"))
count++;
return count;
}