int GetDataSize(char *filename, int **DataSize)
{
FILE * fp, *fp_i;
int c, ndataset;
time_t st,ed;
int n, p, i, flag,ii;
char filename_i[100];
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("can't open input file %s\n",filename);
exit(1);
}
ndataset = 0;
while(!feof(fp)) {
ndataset++;
fscanf(fp, "%s\n", &filename_i);
}
*DataSize = (int *)calloc( ndataset*2, sizeof(int));
ii = 0;
rewind(fp);
while(!feof(fp)) {
ii++;
fscanf(fp, "%s\n", &filename_i);
fp_i = fopen(filename_i, "r");
if(fp_i == NULL)
{
printf("can't open input file %s\n",filename_i);
exit(1);
}
printf("start getting data size of file %d: %s\n", ii, filename_i);
time(&st);
//initialization
if (ii == 1)
{
n = 0;//samples number
// find the number of samples: n
while(1)
{
int c = fgetc(fp_i);//read a character from the data file
switch(c)
{
case '\n'://the end of line
n++;
break;
// fall through,
// count the '-1' element
case EOF://file end
goto out;
default:
;
}
}
}
out:
rewind(fp_i);//Repositions the file pointer to the beginning of a file
// find number of variables: p
p= 0;
i= 0;
flag = 1;
while(1)
{
c = getc(fp_i);
if(c=='\n') goto out2;//end of line
if(isspace(c))
{
flag = 1;
}
/*do {
c = getc(fp);
if(c=='\n') goto out2;//end of line
} while(isspace(c));//space
*/
if (!isspace(c) && (flag==1))
{
p++;//indicate the dimension of the vector
flag = 0;
}
}
out2:
fclose(fp_i);
time(&ed);
// DataSize[0] = n;
(*DataSize)[ndataset * 0 + ii - 1] = n;
(*DataSize)[ndataset * 1 + ii - 1] += p-1;
}
关于(*DataSize)[ndataset * 0 + ii - 1] = n;(*DataSize)[ndataset * 1 + ii - 1] += p-1;不理解,请指教。另外一个问题是(DataSize)[ndataset * 0 + ii - 1] 去掉括号变成DataSize[ndataset * 0 + ii -1],两者之间有什么区别?