鱼C论坛

 找回密码
 立即注册
查看: 2058|回复: 1

C语言读取txt文件中的数据,printf显示读取的数与txt中存的数不一致,怎么回事

[复制链接]
发表于 2017-7-23 11:50:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>

double **array2DI(int col, int row)
{
double **dst;
int i;
dst = (double**)malloc(sizeof(double*)*col);
dst[0] = (double*)malloc(sizeof(double)*row*col);
for (i = 1; i<col; i++)
dst = dst[i - 1] + row;
return dst;
}
void arrayFree2DI(double **array)
{
if (array != NULL)
{
free(array[0]);
free(array);
array = NULL;
}
return;
}

void main()
{
int width = 30;
int height = 20;
double **joint=array2DI(width, height);
FILE *fpRead = fopen("BJ.txt", "r");
if (fpRead == NULL)
{
return 0;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
fscanf(fpRead, "%lf ", &joint[y][x]);
if (y == 10)
{
printf("joint[%d][%d]=%4f\n", y + 1, x + 1, joint[y][x]);
}       
}
}printf("\n");

for (int x = 0; x < width; x++) {
printf("joint[11][%d]=%4f\n", x + 1, joint[10][x]);
}printf("\n");


printf("\n");
fclose(fpRead);
arrayFree2DI(joint);
}

环境是VS2013,结果如图
C:\Users\lenovo\Desktop
我的数组矩阵中第11行的数字,为什么两次printf的结果不一样??
我首先定义了一个二维的动态数组,然后用动态数组去读取txt文件中的数字,一共是30*20个,读取的时候,我用了printf,显示的数字是对的,全部读取完毕后,我又用printf显示了某一行,发现和txt中数字有出入
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-23 11:51:08 | 显示全部楼层
结果图没贴上

结果图

结果图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 01:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表