| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1059 人关注过本帖
标题:我想把数据读入多数组中,再根据需要选出特定的行,要如何实现?
只看楼主 加入收藏
校草
Rank: 1
等 级:新手上路
帖 子:50
专家分:7
注 册:2017-3-15
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:4 
我想把数据读入多数组中,再根据需要选出特定的行,要如何实现?
程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

//跳过6行

void skipline(FILE *fp, int n)  //跳过n行
{
    for (; n>0; n--)
    {
        while (fgetc(fp) != '\n');
    }
}

int main()
{

    //=====================================================
    //基本数据

    int startyear = 2005;//开始年份!!!!!
    int endyear = 2006;//结束年份!!!!!!!
    int stationID = 50136;//气象站ID号!!!!
    int N = 1;            //跳过行数
    const int number = 2;//固定值,文件特殊性质,分上下两个半年
    static int ID[125000];static int Y[125000];static int M[125000];static int D[125000];static int PRE[125000];
    static int a[125000]; static int b[125000]; static int c[125000]; static int d[125000]; static int e[125000];
    static int f[125000]; static int g[125000]; static int h[125000]; static int I[125000]; static int J[125000];
    static int k[125000]; static int l[125000]; static int m[125000]; static int n[125000]; static int o[125000];

    //======================================================
    //文件操作
    char infilename[1000];
    char outfilename[1000];
    FILE *fin, *fout;

    //写入数据
    sprintf(outfilename, "H:\\Heihe_data_all\\中国地面气候资料日值数据集(700 stations 195101-201304)\\桂江气象数据\\sunshine\\"
                            "%d.txt", stationID); //读出位置
    fout = fopen(outfilename, "w+");


    //对文件开始循环
    for (; startyear <= endyear; startyear++){
        for (int j = 1; j <= number; j++){

        sprintf(infilename, "H:\\Heihe_data_all\\中国地面气候资料日值数据集(700 stations 195101-201304)\\桂江气象数据\\"
                    "SURF_CLI_CHN_MUL_DAY_1376058268668_%d_%d.txt", startyear,j);//读入位置!!修改
        fin = fopen(infilename, "r");


        

        //跳过1行

        skipline(fin, N);


        for (int i = 0; i<125000; i++)
        {
            fscanf(fin, "%d,%d,%d,%d,%d,"
                        "%d,%d,%d,%d,%d,"
                        "%d,%d,%d,%d,%d,"
                        "%d,%d,%d,%d,%d",
                        &ID[i],&Y[i],&M[i],&D[i],&PRE[i],
                        &a[i], &b[i], &c[i], &d[i], &e[i],
                        &f[i], &g[i], &h[i], &I[i],&J[i],
                        &k[i], &l[i], &m[i], &n[i],&o[i]);
            if (stationID==ID[i])
            {
                fprintf(fout, "%10d %10d %10d %10d %10d\n", ID[i], Y[i], M[i], D[i], PRE[i]);
            }

        }
               
        fclose(fin);
       
        }
    }
   
    fclose(fout);


    return 0;

}
数据格式是一个20列,122930行的txt数据,类似于如下:
站点     年    月    日    降雨    风速     温度。。。
25121     2005   1     1    100      20       20
25130     2005    1     1     20      53       19



[此贴子已经被作者于2018-11-28 08:54编辑过]

搜索更多相关主题的帖子: 数据 读入 int for static 
2018-11-27 20:34
校草
Rank: 1
等 级:新手上路
帖 子:50
专家分:7
注 册:2017-3-15
收藏
得分:0 
主要是读入数据怎么读不进去呢?我用调试看了一下,所有的数组都是0
2018-11-27 20:36
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10537
专家分:42927
注 册:2014-5-20
收藏
得分:10 
        fin = fopen(infilename, "r");
        if (!infilename){ .................. 这句是什么意思?
            printf("could not open file!");
            exit(99);
        }
2018-11-28 04:37
校草
Rank: 1
等 级:新手上路
帖 子:50
专家分:7
注 册:2017-3-15
收藏
得分:0 
回复 3楼 吹水佬
没打开文件的意思。不记得是从哪抄的了,那我把这句删了吧~~
重点是后面呀,怎读不进去数据,版主大大
2018-11-28 08:30
校草
Rank: 1
等 级:新手上路
帖 子:50
专家分:7
注 册:2017-3-15
收藏
得分:0 
回复 3楼 吹水佬
已经修改了一次,然后,读取出来的都是0,像下面这样:
程序代码:
     50136          0          0          0          0
     50136          0          0          0          0
     50136          0          0          0          0
     50136          0          0          0          0
     50136          0          0          0          0
     50136          0          0          0          0

2018-11-28 08:56
快速回复:我想把数据读入多数组中,再根据需要选出特定的行,要如何实现?
数据加载中...
 
   



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

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