【发布时间】:2012-09-14 02:29:51
【问题描述】:
我正在从文件中读取数字。当我尝试将每个数字放入一个二维数组时,它给了我以下错误。如何摆脱此消息? 我的变量: 文件 *fp; 字符行[80];
错误:从 char * 转换为 int 会丢失精度
代码:-
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char line[80],*pch;
int points[1000][10];
int centroid[1000][10];
float distance[1000][10];
int noofpts=0,noofvar=0,noofcentroids=0;
int i=0,j=0,k;
fp=fopen("kmeans.dat","r");
while(fgets(line,80,fp)!=NULL)
{
j=0;
pch=strtok(line,",");
while(pch!=NULL)
{
points[i][j]=(int)pch;
pch=strtok(NULL,",");
noofvar++;
j++;
}
noofpts++;
i++;
}
noofvar=noofvar/noofpts;
printf("No of points-%d\n",noofpts);
printf("No of variables-%d\n",noofvar);
return 0;
}
【问题讨论】:
-
代码将是帮助定位和修复错误的好工具。
-
向我们展示完整的代码。您似乎正在尝试将指针分配给
int。 -
嗯...要放入堆栈的数据很多。即使你把它送到编译器,当你运行它时它也可能会崩溃。
-
但目前我只拿了 5 个点,即 5x2 数组。
-
那么您是否尝试将字符串表示法中的 int 转换为实际的 int?那个演员只是重新解释了这些位。它不会进行智能转换。
标签: c casting compiler-errors