gpt4 book ai didi

c++ - while (p=strtok(NULL ,",")) 警告 :possible incorrect assignment

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:46:55 25 4
gpt4 key购买 nike

有一个代码,但我收到错误

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 )

关于 array size too large 错误,您应该考虑使用 mallocfree 为 C 中的两个数组使用动态分配,或者使用C++ 中的 std::vector

关于c++ - while (p=strtok(NULL ,",")) 警告 :possible incorrect assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20877826/

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