| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 510 人关注过本帖
标题:各位大侠,帮帮小弟找找错。
只看楼主 加入收藏
linfeng_66
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-27
收藏
 问题点数:0 回复次数:8 
各位大侠,帮帮小弟找找错。
#include "stdio.h"
#include "math.h"
#include "string.h"
#define F float
#define CS 26                             /*cs代表层数*/
main()
{
 FILE *fp;
 int i;
 struct sjb
{int N;
 F UP;
 F DOWN;
 F HD;
 F RXO;
 F RT;
 F ILD;
 int AC;
 F CNL;
 F DEN;
 F SP;
 int GR;
 F CAL;
 F PEF;
 F VSH;
 F PORE;
 int SWE;
 F K;
 char result[4];                            /*建立数据表结构体*/  
 };
 struct sjb pu[CS];
 struct sjb *p;
 p=&pu[CS];
 fp=fopen ("sjb1.txt","r");
 if (fp==NULL)
 printf("cannot open this file\n");
 else
 printf("12324234444");
       for(i=1;i<CS;i++)
      {
       fread(&pu[i],sizeof(struct sjb),1,fp);
       printf("%f",p->CAL);
       }
    }


程序本意是想读取sjb1.txt文件的内容,然后赋值给结构体数组pu[CS]. 但p->CAL,P->DEN等的值都是零,不知道为啥?本人刚开始学习,希望大家别笑话。
sjb1.txt的文件如下:
1    910.8    912.2    1.4    3.8    5.5    5.2    111    33.6    2.17    2    110    9    2.1    15.1    29.7    100    34.73    s
2    920.4    923.2    2.8    4.5    6.3    6.6    102    30.1    2.2    1    124    9.1    2.2    15.8    26.6    100    21.35    s
3    934.8    939    4.2    3.9    5    4.4    115    31.3    2.17    1    111    9.3    1.8    14.8    30.3    100    126.44    s
4    956.8    957.4    0.6    8.7    4.1    2.9    101    35.7    2.47    0    104    9.2    2.5    47.8    8.4    100    0.01    g
5    961.8    965    3.2    3.6    4.4    5.4    109    29    2.17    1    122    9.1    2.1    18.2    27.5    100    85.11    s
6    967.8    969.2    1.4    4    4.2    4    102    31.1    2.2    1    118    9.5    2.1    29.1    22.9    100    16.98    s
7    989.2    991.8    2.6    4    5.3    6.3    114    27.1    2.2    1    111    9.2    1.7    13.8    28.6    100    136.97    s
8    997    997.8    0.8    3.3    3.8    4.7    99    28.9    2.38    1    105    9.6    3    53    6.1    100    0.01    g
9    998.8    1000    1.2    4.6    5.9    6.3    111    26.3    2.2    1    110    9.4    2.1    32.1    16.8    100    57.97    s
10    1022.4    1023.2    0.8    5    4.6    5.5    89    22.9    2.42    0    118    9.6    2.7    50.4    5.7    100    0.01    g
11    1130.4    1131    0.6    10.5    6.2    7.7    88    26.1    2.37    0    112    9.3    2.5    48.5    6.4    100    0.01    g
12    1147    1147.8    0.8    18.1    6    4.5    90    27.6    2.43    0    104    9.9    2.3    49    7    100    0.01    g
13    1176.6    1177.4    0.8    9.9    9.3    8.8    84    20.8    2.33    0    118    9.5    2.3    38.4    6.5    100    0.03    g
14    1210    1211.8    1.8    4.2    5.3    6.1    107    28.1    2.21    3    124    8.6    2    17.7    26.3    100    196.83    s
15    1214.8    1215.4    0.6    34.8    7.2    4.7    91    23.4    2.45    0    125    8.7    2.6    44.3    7.7    100    0.43    g
16    1220.4    1221.2    0.8    3.8    4.4    4.3    107    29.3    2.22    2    129    8.7    2.3    25.9    24.6    100    78.58    s
17    1223.2    1227.2    4    4.1    5.7    6.8    104    27.3    2.2    4    115    8.8    2.1    13.7    27.1    100    312.81    s
18    1241    1244.6    3.6    4    6.7    7.8    101    24.1    2.21    2    108    8.6    1.9    8.8    26.1    100    403.39    s
19    1246.8    1250.2    3.4    3.4    6.2    7    108    29.9    2.2    3    122    8.6    2    14.1    27.8    100    331.47    s
20    1271    1279    8    4.1    7.2    8.7    107    28.8    2.2    1    117    8.7    1.9    9.8    28    100    85.31    s
21    1281.4    1282.4    1    45.9    19.5    8.3    81    16.6    2.36    1    108    8.8    2.2    24.6    8.2    100    0.14    g
22    1289.8    1294    4.2    2.8    7.4    8.7    116    28.7    2.12    2    118    8.8    1.8    1.2    32.4    100    448.41    s
23    1296    1310.6    14.6    5.6    5.6    4.8    100    27.7    2.25    2    118    8.8    1.9    20.9    23.5    100    17.46    s
24    1754    1754.6    0.6    10.4    6.1    4.8    93    34.9    2.55    0    100    8.8    3.2    44.1    7.5    100    0.06    g
25    1762.2    1763.4    1.2    10.1    12.7    10.3    79    24.9    2.59    0    68    8.9    3.6    35    8    100    0.04    g
26    1770.4    1771.2    0.8    23.5    12.6    8    80    27.6    2.62    1    77    8.8    2.7    40.4    7.6    100    0.02    g
搜索更多相关主题的帖子: 大侠 
2008-05-27 21:28
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1696
专家分:4295
注 册:2007-12-27
收藏
得分:0 
这种文件不能fread
建议fscanf
2008-05-27 22:19
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
收藏
得分:0 
零是它们的初值
还有,for(i=1;i<CS;i++)
你的循环只能读25组
2008-05-27 23:38
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
收藏
得分:0 
按照#2的提示,我做出来了
else
{
printf("12324234444\n");
fseek( fp, 0L, SEEK_SET );
       for(i=0;i<CS;i++)
          {
                        fscanf(fp, "%d", &pu[i].N);
                        fscanf(fp, "%f", &pu[i].UP);
            fscanf(fp, "%f", &pu[i].DOWN);
            fscanf(fp, "%f", &pu[i].HD);
            fscanf(fp, "%f", &pu[i].RXO);
            fscanf(fp, "%f", &pu[i].RT);
            fscanf(fp, "%f", &pu[i].ILD);
                                   fscanf(fp, "%d", &pu[i].AC);
           fscanf(fp, "%f", &pu[i].CNL);
           fscanf(fp, "%f", &pu[i].DEN);
           fscanf(fp, "%f", &pu[i].SP);
           fscanf(fp, "%d", &pu[i].GR);
           fscanf(fp, "%f", &pu[i].CAL);
           fscanf(fp, "%f", &pu[i].PEF);
           fscanf(fp, "%f", &pu[i].VSH);
           fscanf(fp, "%f", &pu[i].PORE);
           fscanf(fp, "%d", &pu[i].SWE);
           fscanf(fp, "%f", &pu[i].K);
           fscanf(fp, "%s", &pu[i].result);
           }
       for(i = 0; i<CS; i++)
       {
           printf("%d ",pu[i].N);
           printf("%.1f ",pu[i].UP);
           printf("%.1f ",pu[i].DOWN);
           printf("%.1f ",pu[i].HD);
           printf("%.1f ",pu[i].RXO);
           printf("%.1f ",pu[i].RT);
           printf("%.1f ",pu[i].ILD);
           printf("%d ",pu[i].AC);
           printf("%.1f ",pu[i].CNL);
           printf("%.2f ",pu[i].DEN);
           printf("%.1f ",pu[i].SP);
           printf("%d ",pu[i].GR);
           printf("%.1f ",pu[i].CAL);
           printf("%.1f ",pu[i].PEF);
           printf("%.1f ",pu[i].VSH);
           printf("%.1f ",pu[i].PORE);
           printf("%d ",pu[i].SWE);
           printf("%.2f ",pu[i].K);
           printf("%s ",pu[i].result);
           printf("\n");
       }
}
fclose(fp);
}
今天又有收获,学会了读取文件
谢谢LZ,谢谢#2
2008-05-28 00:54
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
Orz………………你不能写成一条语句吗……

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-05-28 05:29
linfeng_66
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-27
收藏
得分:0 
感谢以上个位
有个问题,为啥以上数据不能用fread读?是因为格式不对吗?
2008-05-28 09:17
linfeng_66
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-27
收藏
得分:0 
用了luck boy 的方法,还是不行啊?
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "math.h"
#include "string.h"
#define F float
#define CS 26                             /*cs代表层数*/
main()
{
 FILE *fp;
 int i;
 struct sjb
{int N;
 F UP;
 F DOWN;
 F HD;
 F RXO;
 F RT;
 F ILD;
 int AC;
 F CNL;
 F DEN;
 F SP;
 int GR;
 F CAL;
 F PEF;
 F VSH;
 F PORE;
 int SWE;
 F K;
 char result[4];                            /*建立数据表结构体*/  
 };
 struct sjb pu[CS];
 struct sjb *p;
 p=&pu[CS];
 fp=fopen ("sjb1.txt","r");
 if (fp==NULL)
 printf("cannot open this file\n");
 else
{
printf("12324234444\n");
fseek( fp, 0L, SEEK_SET );
       for(i=0;i<CS;i++)
          {
                                fscanf(fp, "%d", &pu[i].N);
                                fscanf(fp, "%f", &pu[i].UP);
                        fscanf(fp, "%f", &pu[i].DOWN);
                        fscanf(fp, "%f", &pu[i].HD);
                        fscanf(fp, "%f", &pu[i].RXO);
                        fscanf(fp, "%f", &pu[i].RT);
                        fscanf(fp, "%f", &pu[i].ILD);
                                   fscanf(fp, "%d", &pu[i].AC);
                   fscanf(fp, "%f", &pu[i].CNL);
                   fscanf(fp, "%f", &pu[i].DEN);
                   fscanf(fp, "%f", &pu[i].SP);
                   fscanf(fp, "%d", &pu[i].GR);
                   fscanf(fp, "%f", &pu[i].CAL);
                   fscanf(fp, "%f", &pu[i].PEF);
                   fscanf(fp, "%f", &pu[i].VSH);
                   fscanf(fp, "%f", &pu[i].PORE);
                   fscanf(fp, "%d", &pu[i].SWE);
                   fscanf(fp, "%f", &pu[i].K);
                   fscanf(fp, "%s", &pu[i].result);
           }
           for(i = 0; i<CS; i++)
           {
                   printf("%d ",pu[i].N);
                   printf("%.1f ",pu[i].UP);
                   printf("%.1f ",pu[i].DOWN);
                   printf("%.1f ",pu[i].HD);
                   printf("%.1f ",pu[i].RXO);
                   printf("%.1f ",pu[i].RT);
                   printf("%.1f ",pu[i].ILD);
                   printf("%d ",pu[i].AC);
                   printf("%.1f ",pu[i].CNL);
                   printf("%.2f ",pu[i].DEN);
                   printf("%.1f ",pu[i].SP);
                   printf("%d ",pu[i].GR);
                   printf("%.1f ",pu[i].CAL);
                   printf("%.1f ",pu[i].PEF);
                   printf("%.1f ",pu[i].VSH);
                   printf("%.1f ",pu[i].PORE);
                   printf("%d ",pu[i].SWE);
                   printf("%.2f ",pu[i].K);
                   printf("%s ",pu[i].result);
                   printf("\n");
                  
           }
}
fclose(fp);
printf("%f",pu[i].CAL);
}
运行结果为12324234444
scanf:floating point formats not linked abnormal programe termination
2008-05-28 09:51
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
上面的:用TC了吧?你随便调用一个float函数,比如sqrt,TC默认是使用非浮点支持的函数的,为了节省空间,必须要让TC认为你使用了浮点数才行。定义一个float或者随便使用一个浮点函数就可以了。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-05-28 11:11
linfeng_66
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-27
收藏
得分:0 
thanksssssss
感谢所有的给我指点的人,小弟成功了,非常感谢!!!!
2008-05-28 18:05
快速回复:各位大侠,帮帮小弟找找错。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030207 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved