有一个代码,但我收到错误
Line 8: array size too large
line 9: array size too large
line 28 while (p=strtok(NULL,",")) warning :possible incorrect assignment
那么你能建议我如何处理这个警告/错误吗?
代码:
/* convert csv data to libsvm/svm-light format */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[10000000];
float feature[100000];
int main(int argc, char **argv)
{
FILE *fp;
if(argc!=2) { fprintf(stderr,"Usage %s filename\n",argv[0]); }
if((fp=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"Can't open input file %s\n",argv[1]);
}
while(fscanf(fp,"%[^\n]\n",buf)==1)
{
int i=0,j;
char *p=strtok(buf,",");
feature[i++]=atof(p);
while((p=strtok(NULL,",")))
feature[i++]=atof(p);
// --i;
/*
if ((int) feature[i]==1)
printf("-1 ");
else
printf("+1 ");
*/
// printf("%f ", feature[1]);
printf("%d ", (int) feature[0]);
for(j=1;j<i;j++)
printf(" %d:%f",j,feature[j]);
printf("\n");
}
return 0;
}
最佳答案
您实际上希望将 strtok
返回的值存储到变量 p
中,同时检查该值是否为 NULL。
我建议您按照以下方式编写,与当前的编写方式相比,这种方式是等效的,并且更具描述性。
while( ( p=strtok(NULL,",") )!=NULL )
关于数组大小太大错误,您应该考虑对 C 中的两个数组使用 malloc
和 free
进行动态分配,或者使用C++ 中的 std::vector
。
关于c++ - 而(p = strtok(NULL ,","))警告:possible incorrect assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20877826/