| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2796 人关注过本帖
标题:c语言怎样按照行读取多组(如169个)浮点数据?
只看楼主 加入收藏
xuanyz11
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-1-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
c语言怎样按照行读取多组(如169个)浮点数据?
我有一组169个按行存储浮点数据,      !!!!注意一定要按行来获取数据!!!!!!
,如
 -0.041534  0.075458  0.125884 -0.119297 -0.109003 -0.005356 -0.035634 -0.069634  0.014044  0.187113 -0.207388 -0.003791  0.038806 -0.086751 -0.227366  0.127539  0.014591 -0.101418 -0.046260  0.035553 -0.009294 -0.032989  0.047126 -0.230441  0.016459 -0.057562  0.214803  0.055086 -0.196953 -0.125830  0.055267  0.039813 -0.149408  0.036923  0.068638 -0.114248 -0.129709  0.141893 -0.033887  0.093024 -0.088547  0.012981 -0.202999 -0.077986  0.023384  0.150309 -0.163984 -0.239862 -0.082718  0.039668 -0.092940 -0.005184  0.205974 -0.044229  0.172142 -0.226272 -0.075567  0.060631 -0.108353  0.160511  0.023597  0.065444  0.129215 -0.035263 -0.010744 -0.093184 -0.080180  0.024636 -0.016579 -0.027837 -0.011008  0.051407 -0.170007  0.198092 -0.064775 -0.018346 -0.196314  0.070390  0.033234 -0.022613  0.066513 -0.136225  0.106919 -0.137492  0.104262 -0.034337 -0.132523 -0.088268  0.078946 -0.138155  0.040438  0.174366 -0.185534  0.165147 -0.001113 -0.089207 -0.301762  0.023248 -0.095162 -0.153458 -0.107724  0.048973  0.033003  0.030922 -0.004928  0.066224 -0.130826  0.175129 -0.056769  0.011604  0.138880  0.215768 -0.101236 -0.178283 -0.010201 -0.135965  0.136387  0.109390 -0.077946  0.183571 -0.196223  0.135005 -0.015047  0.096648  0.077280 -0.277536  0.002396 -0.009316 -0.120833 -0.290003  0.023691  0.027691  0.047670 -0.078493 -0.127261  0.031569  0.068580  0.068191  0.275069 -0.061078  0.103908  0.086652  0.040250 -0.088237 -0.135148 -0.199208  0.006514  0.112101  0.076940  0.048842  0.110466  0.138432 -0.112149 -0.021517 -0.162104 -0.072641  0.023116 -0.022867  0.139739 -0.020996 -0.144180 -0.131307 -0.061731  0.004564 -0.001211  0.040581 -0.153690  0.053347  0.163220
现在需要将其利用c语言读入并存储到相应的一维数组中,我的大致思路是:
先将数据按照字符串读入,
再将字符串中的数值按浮点数据读入数组
最后在屏幕输出
具体程序如下:

int main()
   {
          char filename[] = "data.txt"; //文件名
           FILE *fp;
       int i;
       float  data;//temporary values for reading data
          char StrLine[1690];                                 //每行最大读取的字符数
       float Znode[169];//待存储数据的一维数组

       if((fp = fopen(filename,"r")) == NULL) //判断文件是否存在及可读
       {
         printf("error!");
          return -1;
       }

         fgets(StrLine,1690,fp);  //读取一行
data.rar (1.07 KB)

         printf("%s\n", StrLine); //屏幕输出读取的字符串
         for (i=0;i<169;i++)
         {
                   sscanf(StrLine,"%f",&data);//从字符串读取浮点数据
                Znode[i] = data;//将浮点数据存入一维数组
                printf("%9.6f\n",Znode[i]);//屏幕显示数组元素
         }

        fclose(fp);                     //关闭文件
     return 0;
  }

但输出的结果 是不断的输出第一个数值-0.041534,而并没有将第2个到第169个数据存入矩阵Znode中,我没有相同原因,请教高手指教指教,或者还有更好的思路也可以,多谢了

[此贴子已经被作者于2019-1-9 21:10编辑过]

搜索更多相关主题的帖子: c语言 读取 浮点数 字符串 data 
2019-01-09 21:04
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:15 
#include <stdio.h>
int main()
{
    char *s=" -0.041534  0.075458  0.125884 -0.119297 -0.109003 -0.005356 -0.035634 -0.069634  0.014044  0.187113 -0.207388 -0.003791  0.038806 -0.086751 -0.227366  0.127539  0.014591 -0.101418 -0.046260  0.035553 -0.009294 -0.032989  0.047126 -0.230441  0.016459 -0.057562  0.214803  0.055086 -0.196953 -0.125830  0.055267  0.039813 -0.149408  0.036923  0.068638 -0.114248 -0.129709  0.141893 -0.033887  0.093024 -0.088547  0.012981 -0.202999 -0.077986  0.023384  0.150309 -0.163984 -0.239862 -0.082718  0.039668 -0.092940 -0.005184  0.205974 -0.044229  0.172142 -0.226272 -0.075567  0.060631 -0.108353  0.160511  0.023597  0.065444  0.129215 -0.035263 -0.010744 -0.093184 -0.080180  0.024636 -0.016579 -0.027837 -0.011008  0.051407 -0.170007  0.198092 -0.064775 -0.018346 -0.196314  0.070390  0.033234 -0.022613  0.066513 -0.136225  0.106919 -0.137492  0.104262 -0.034337 -0.132523 -0.088268  0.078946 -0.138155  0.040438  0.174366 -0.185534  0.165147 -0.001113 -0.089207 -0.301762  0.023248 -0.095162 -0.153458 -0.107724  0.048973  0.033003  0.030922 -0.004928  0.066224 -0.130826  0.175129 -0.056769  0.011604  0.138880  0.215768 -0.101236 -0.178283 -0.010201 -0.135965  0.136387  0.109390 -0.077946  0.183571 -0.196223  0.135005 -0.015047  0.096648  0.077280 -0.277536  0.002396 -0.009316 -0.120833 -0.290003  0.023691  0.027691  0.047670 -0.078493 -0.127261  0.031569  0.068580  0.068191  0.275069 -0.061078  0.103908  0.086652  0.040250 -0.088237 -0.135148 -0.199208  0.006514  0.112101  0.076940  0.048842  0.110466  0.138432 -0.112149 -0.021517 -0.162104 -0.072641  0.023116 -0.022867  0.139739 -0.020996 -0.144180 -0.131307 -0.061731  0.004564 -0.001211  0.040581 -0.153690  0.053347  0.163220";
    char *p=s;
    int n;
    float a;
    while (*p)
    {
        if (sscanf(p, "%f%n", &a, &n) == 1)
        {
            printf("%f\n", a);
            p += n;
        }
        else
            ++p;
    }
    return 0;
}
2019-01-09 21:16
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:396
专家分:2640
注 册:2018-3-30
收藏
得分:5 
程序代码:
#include<stdio.h>
int main()
{
    double x,data[1000];
    int i=0;
    freopen("data.txt", "r", stdin);
    while(scanf("%lf", &x) == 1) {
        data[i++]=x;
        printf("%lf  ",x);
    }
    return 0;
}

saber,别哭.
2019-01-09 22:50
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
回复 3楼 幻紫灵心
你这么写不满足“一行”的要求2333

专心编程………
飞燕算法初级群:3996098
我的Blog
2019-01-10 00:30
xuanyz11
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-1-9
收藏
得分:0 
感谢各位的热心回复,我先试试看
2019-01-10 09:00
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:396
专家分:2640
注 册:2018-3-30
收藏
得分:0 
回复 4楼 StarWing83
他测试数据就只有一行,偷个懒,哈哈

saber,别哭.
2019-01-10 14:37
xuanyz11
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-1-9
收藏
得分:0 
感谢幻紫灵心和吹水佬的回复,两个程序测试单行都没有问题,如果是多行每行都有169个数据又怎么处理呢?
我是初学者,再次感谢了!
例如是三行 数据如下:
 -0.041534  0.075458  0.125884 -0.119297 -0.109003 -0.005356 -0.035634 -0.069634  0.014044  0.187113 -0.207388 -0.003791  0.038806 -0.086751 -0.227366  0.127539  0.014591 -0.101418 -0.046260  0.035553 -0.009294 -0.032989  0.047126 -0.230441  0.016459 -0.057562  0.214803  0.055086 -0.196953 -0.125830  0.055267  0.039813 -0.149408  0.036923  0.068638 -0.114248 -0.129709  0.141893 -0.033887  0.093024 -0.088547  0.012981 -0.202999 -0.077986  0.023384  0.150309 -0.163984 -0.239862 -0.082718  0.039668 -0.092940 -0.005184  0.205974 -0.044229  0.172142 -0.226272 -0.075567  0.060631 -0.108353  0.160511  0.023597  0.065444  0.129215 -0.035263 -0.010744 -0.093184 -0.080180  0.024636 -0.016579 -0.027837 -0.011008  0.051407 -0.170007  0.198092 -0.064775 -0.018346 -0.196314  0.070390  0.033234 -0.022613  0.066513 -0.136225  0.106919 -0.137492  0.104262 -0.034337 -0.132523 -0.088268  0.078946 -0.138155  0.040438  0.174366 -0.185534  0.165147 -0.001113 -0.089207 -0.301762  0.023248 -0.095162 -0.153458 -0.107724  0.048973  0.033003  0.030922 -0.004928  0.066224 -0.130826  0.175129 -0.056769  0.011604  0.138880  0.215768 -0.101236 -0.178283 -0.010201 -0.135965  0.136387  0.109390 -0.077946  0.183571 -0.196223  0.135005 -0.015047  0.096648  0.077280 -0.277536  0.002396 -0.009316 -0.120833 -0.290003  0.023691  0.027691  0.047670 -0.078493 -0.127261  0.031569  0.068580  0.068191  0.275069 -0.061078  0.103908  0.086652  0.040250 -0.088237 -0.135148 -0.199208  0.006514  0.112101  0.076940  0.048842  0.110466  0.138432 -0.112149 -0.021517 -0.162104 -0.072641  0.023116 -0.022867  0.139739 -0.020996 -0.144180 -0.131307 -0.061731  0.004564 -0.001211  0.040581 -0.153690  0.053347  0.163220
 -0.134117 -0.086829  0.061958 -0.052394  0.295536  0.188160  0.079704 -0.035226  0.139109  0.044362 -0.182218  0.151328 -0.103277  0.094604  0.001721 -0.006943 -0.044203 -0.062622 -0.108095 -0.122620  0.027528 -0.039896 -0.011545  0.079012 -0.064183 -0.013317  0.002569  0.083743  0.180786  0.066421 -0.016446  0.053584  0.022818 -0.089484 -0.131985  0.037843 -0.263373  0.019091 -0.069334  0.198532  0.160447 -0.009362 -0.229999  0.021441 -0.121561  0.099873 -0.113858  0.006691  0.066946 -0.054711 -0.122417  0.090886 -0.013649  0.139712 -0.206368 -0.240182  0.024073  0.056228 -0.090998 -0.035578  0.116716 -0.030448  0.160300 -0.034920 -0.137226  0.041696 -0.079389  0.036509 -0.237632 -0.128846  0.028167 -0.222799  0.090214 -0.154658 -0.031241  0.025389 -0.006010  0.013570  0.129806 -0.071247 -0.026180  0.087035 -0.002903 -0.012586 -0.033694  0.002341 -0.041949 -0.200133  0.246165 -0.078502 -0.276136  0.091640  0.186482 -0.106184  0.132771  0.010412  0.061972 -0.031399 -0.005662 -0.056397  0.034648 -0.142948 -0.191311  0.053223  0.041112  0.171993 -0.036758 -0.062234  0.017970 -0.051600 -0.056213  0.097354  0.014529  0.137210  0.137742  0.017778 -0.032607  0.111508  0.143601  0.086455  0.151277  0.033308  0.123376  0.127286  0.051972  0.064619  0.013670 -0.035828  0.056896  0.029342 -0.023505 -0.015468 -0.122755 -0.135918 -0.164249 -0.100438 -0.099561  0.068961  0.091561 -0.128725 -0.162326 -0.095639 -0.108396  0.174384 -0.036334 -0.016891 -0.038591 -0.043431  0.102315  0.016401  0.124877 -0.056640  0.077522  0.053545 -0.132202  0.184064  0.051298  0.031146  0.141108  0.164472 -0.039753  0.111214 -0.020217 -0.066110  0.079085  0.117651 -0.228759 -0.007559 -0.106399
 -0.125643  0.015604  0.164026 -0.096082 -0.118545 -0.139447 -0.065565  0.046639 -0.037156 -0.081089 -0.083507  0.032962  0.055129 -0.098122  0.046805 -0.087805 -0.017789  0.084166 -0.076136 -0.040948  0.131509 -0.048433 -0.064912 -0.115269 -0.123892  0.116906  0.129787 -0.036053 -0.058462  0.178677 -0.006252 -0.092104 -0.186411 -0.264627 -0.085608 -0.121410 -0.120320 -0.036663 -0.058975  0.150744 -0.148993 -0.102454  0.235659  0.097445 -0.223122 -0.187286 -0.054026 -0.119296  0.157416  0.096259 -0.089798  0.113346  0.027596  0.008566 -0.047756  0.055754  0.112948  0.049755  0.008704 -0.017357 -0.213443  0.043727  0.128891  0.056026 -0.000640  0.039791  0.024000 -0.125606  0.124444 -0.025450  0.194154  0.163278  0.098413  0.010015 -0.212967 -0.155051  0.083278  0.195496  0.091683  0.095751 -0.097061 -0.095923 -0.082327 -0.049340  0.057520 -0.026008 -0.122171  0.135205 -0.192598  0.116438  0.134162  0.135176 -0.009181  0.042613 -0.278353  0.225287 -0.131847  0.027230  0.154740 -0.108670 -0.173177  0.119598 -0.095103 -0.141168 -0.083864 -0.081790  0.035759 -0.048305  0.072698  0.035892 -0.192291  0.106523 -0.217703  0.071138  0.058979  0.147217 -0.116833 -0.039449  0.049952 -0.102323 -0.138902  0.204254  0.280750 -0.029271  0.103508  0.062630 -0.100947  0.077570 -0.016516  0.035801 -0.144203 -0.070603 -0.224260  0.060370  0.087586  0.026333  0.093102 -0.039760 -0.032493 -0.033130 -0.029454  0.122827  0.108385 -0.086979  0.020898  0.033923 -0.116694 -0.198612 -0.157349  0.080945  0.017800  0.132096 -0.064073 -0.024909 -0.002343 -0.285289  0.011923 -0.121627 -0.127815 -0.049015 -0.105460  0.065504  0.254208 -0.011403  0.196604  0.134979  0.178704  0.214966  0.065793
2019-01-10 19:33
xuanyz11
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-1-9
收藏
得分:0 
回复 2楼 吹水佬
烦劳请问一下,程序中:if (sscanf(p, "%f%n", &a, &n) == 1)
一行中%n作何理解呢?
2019-01-10 19:35
我是锤头
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2018-12-12
收藏
得分:0 
回复 4楼 StarWing83
求大仙告诉一下按行的要求~~!!
2019-01-11 15:22
快速回复:c语言怎样按照行读取多组(如169个)浮点数据?
数据加载中...
 
   



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

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