gpt4 book ai didi

c - 输入文件时出现 SegFault

转载 作者:行者123 更新时间:2023-11-30 14:19:52 27 4
gpt4 key购买 nike

这是我的代码。编辑:调用者包含在底部。

该函数读取数据文件,确定有多少行和列,然后将数据存储到 data_array 中。

int getdata(double* *data_array, int* *data_labels, int argc, char *argv[], int *items, int *attr)
{
// filename variables
char *filename; // pointer to a string that will contain the name of the training data file.
char *result = NULL; // used with strtok() to extract each feature value given a line of delimited features.
FILE *fp; // pointer to FILE, we can use this with fgets to access each line
char line[10000]; // array of 1000 chars for storing the raw data for one observation
char delims[4]; // an array of common delimiters for data files
delims[0]='\t';
delims[1]=' ';
delims[2]=',';
delims[3]='\0';
int i, j;
// check that we have the correct number of command line arguments
if (argc < 2)
{
printf("2usage: progname filename\n");
return -1;
}

if (argc < 4)
{
printf("3usage: progname filename num_labels k(nn)\n");
return -1;
}

if (atoi(argv[2]) < 1)
{
printf("num_labels must be a positive integer.\n");
return -1;
}

if (atof(argv[2]) - atoi(argv[2]) > 0)
{
printf("num_labels must be an integer.\n");
return -1;
}

if (atoi(argv[3]) < 1)
{
printf("k must be a positive integer.\n");
return -1;
}

if (atof(argv[3]) - atoi(argv[3]) > 0)
{
printf("k must be an integer.\n");
return -1;
}

// try to open the file
filename = argv[1];
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("could not open file: %s\n", filename);
printf("note: the filename should be the second command line argument, after the .exe file");
return -1;
}
printf("reading file: %s\n", filename);

// GET NUMBER OF ROWS AND COLUMNS OF INPUT FILE
fgets(line, 10000, fp);
result = strtok(line,delims);
while(result != NULL)
{
(*items)++;
result = strtok(NULL, delims);
}
(*attr)++;
while(fgets(line,sizeof(line),fp) != NULL)
(*attr)++;

printf("num items: %d\n", *items);
printf("num attributes: %d\n", *attr);


// NOW THAT YOU HAVE FILE SIZE, ALLOCATE MEMORY TO STORE DATA
*data_array = malloc(*items* *attr*sizeof(double));
//*data_labels = malloc(*items*sizeof(int));
printf("data array size = %d\n\n",*items* *attr);

i=0;
j=0;
fclose(fp);
fp = fopen(filename, "r");
// SAVE DATA INTO DATA_ARRAY
while (fgets(line, 10000, fp) != NULL)
{

// we break line into tokens using our delimeters list declared at the beginning of the function
result = strtok(line,delims);
//printf("%d\n",i);
while(result != NULL) {
(*data_array)[i++] = atof(result);
//printf("%f\n",(*data_array)[i-1]);
result = strtok(NULL, delims);
}
//(*data_labels)[j++] = (int)((*data_array)[--i]);
}
// close the file
fclose(fp);
return 0;
}

int main(int argc, char *argv[])
{
double *data_array = NULL;
int *data_labels = NULL;
int items = 0;
int attr = 0;

if (getdata(&data_array, &data_labels, argc, argv, &items, &attr) == -1)
{
printf("reading input data failed");
return 0;
}
printf("PROCESS COMPLETED WITHOUT SEGFAULT");

#ifdef CROSSVAL
// v-fold cross-validation
if (crossval(data_array, atoi(argv[2]), items, attr, closest_center, centers) == -1)
{
printf("k-means operation failed");
return 0;
}
#else

// coords holds coordinates of one new point
int i;
double coords[attr];
for(i = 0; i<attr;i++)
{
coords[i] = atof(argv[i+4]);
}
/*
// calling knn function
if (knn(data_array, data_labels, atoi(argv[2]), atoi(argv[3]), coords, items, attr) == -1) // use a heap!
{
printf("knn operation failed");
return 0;
}
*/
#endif

free(data_array);
return 0;
}

奇怪的是这个函数在出现段错误之前输出正确。给定一个包含 2 列和 272 行的数据文件,我得到“num items: 2, num attribute: 272, data array size = 544”。但是,如果我注释掉 "printf("num attribute: ..."行之后和最后一个 fclose(fp) 行之前的所有内容,它仍然会出现段错误。它很好地完成了打印语句,然后出现了段错误,即使它后面没有重要的代码。太困惑了。

最佳答案

for 循环中调用 getdata() 后,程序出现段错误。

您不会检查程序是否有足够的参数,也不会读取 attr + 4 参数,无论它们是否存在。

这也是当您删除 (*attr)++ 时不会出现段错误的原因。因为这样,attr 仍为 0 并且您不会尝试读取不存在的参数。

关于c - 输入文件时出现 SegFault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971220/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com