C语言 怎样读取文件 按行读入数字 挨个转换成double类型存入数组, 求审代码。
int main(){
FILE *fp;
char buffer[50];
char filename[50];
int bufferLen;
int i;
int j = 0;
char c;
double c_number[50];
printf("Input file name: ");
scanf("%s", filename);
if((fp = fopen(filename, "rb")) == NULL)
{
return -1;
}
//用的fgets 按行读取
while(fgets(buffer, 50, fp))
{
for(i = 0; i < 50; i++)
{
c = buffer[i];
//过滤字符
if(c != ' ' && c != '\n' && c != '\r')
{
//尝试转换字符
c_number[j] = (double)atof(c);
//sscanf(c, "%lf", c_number[j]);
j++;
}
}
}
fclose(fp);
---------------------- Table -----------------------------------------------
Column_1: 160,591,114,229,230,270,128,1657,624,1503
Column_2: 15.0,69.9,6.5,22.4,28.4,65.9,19.4,198.7,38.8,138.2
[ 本帖最后由 cevin880221 于 2015-4-1 10:41 编辑 ]