gpt4 book ai didi

c - 尝试将文件解析为数组但获得额外的数字

转载 作者:行者123 更新时间:2023-11-30 16:49:41 26 4
gpt4 key购买 nike

我有一个充满这种格式数据的文本文件(它是 3D 图形编辑器 .vox 格式的输出)...

6 -13 8 eeeeec
13 -13 8 eeeeec
6 -12 8 eeeeec
6 -11 8 eeeeec
6 -10 8 eeeeec
1 -9 8 eeeeec
2 -9 8 eeeeec
3 -9 8 eeeeec

我正在使用以下代码将整数解析为数组...

#include<stdio.h>

int array[10000];
char *p;

int main()
{
FILE *ptr_file;
char buf[10];

ptr_file =fopen("AntAttackMap.txt","r");
if (!ptr_file)
return 1;

long index = 0;

while (fgets(buf,10, ptr_file)!=NULL)
{
p = strtok(buf, " ec");

while (p != NULL)
{

int num = atoi(p);
array[index]=num;
printf ("%d ",num);
p = strtok (NULL, " ec");
if (p != NULL) index++;
}
}

fclose(ptr_file);
printf("TOTAL %d, ",index);
return 0;
}

但是,输出的 3 位数字之间有额外的零,如下所示:

6 -13 8 0 13 -13 8 0 6 -12 8 0 6 -11 8 0 6 -10 8 0 1 -9 8 0 2 -9 8 0 3 -9 8 0

有人可以解释一下为什么我会得到额外的数字吗?

提前致谢,

迈克

最佳答案

您可以使用BUFSIZ常量,示例

char buf[BUFSIZ];

关于c - 尝试将文件解析为数组但获得额外的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494795/

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