简体   繁体   English

读取 3 个文件并从中制作 arrays

[英]Reading 3 files and making arrays out of them

I have three text files which contains a list of lines and each line is comprised a latitude longitude, and value.我有三个文本文件,其中包含一个行列表,每行包含一个纬度经度和值。 These arrays cover different ranges of latitude and longitude.这些 arrays 涵盖不同的经纬度范围。 Now what I want to make three different latitude arrays, three different longitude arrays, and three different value arrays where each text file is supposed to generate one latitude,longitude and value array each.现在我想要制作三个不同的纬度 arrays、三个不同的经度 arrays 和三个不同的值 arrays,其中每个文本文件应该生成一个纬度和经度值的每个数组。

Below I have the code in c in which I am attempting to accomplish this:下面我在 c 中有代码,我试图在其中完成此操作:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>


int main(int argc, char *argv[])
{
    FILE    *fp=NULL, *fpb=NULL, *fpc=NULL, *FPOUT=NULL;
    char    inpFname[81],inpFnameb[81],inpFnamec[81],FILEOUT[81];
    int     i,j,k;
    char    buf[8000];
    int     count;
    char    datetime[10],year[4],month[2],day[2],hour[2],type[10];

    float   latnum,lonnum,valnum;
    float   latnumb,lonnumb,valnumb;
    float   latnumc,lonnumc,valnumc;

    float*  latarray = malloc(sizeof(float) * 1038240);
    float*  lonarray = malloc(sizeof(float) * 1038240);
    float*  valarray = malloc(sizeof(float) * 1038240);

    float*  latarrayb = malloc(sizeof(float) * 3185);
    float*  lonarrayb = malloc(sizeof(float) * 3185);
    float*  valarrayb = malloc(sizeof(float) * 3185);

    float*  latarrayc = malloc(sizeof(float) * 5265);
    float*  lonarrayc = malloc(sizeof(float) * 5265);
    float*  valarrayc = malloc(sizeof(float) * 5265);


    sprintf(datetime,"%s",argv[1]);


    sprintf(inpFname,"100_%s.txt1",datetime);
    printf("%s\n",inpFname);

    if ((fp=fopen(inpFname,"rt"))==NULL)
    {
        printf("\nERROR: Cannot open/read input file [%s]\n\n",inpFname);
        exit(1);
    } 

    i=0;
    while(i < 1038240 && fgets(buf,sizeof buf, fp))
    {
        sscanf(buf,"%f %f %f\n",&latnum,&lonnum,&valnum);
    
        latarray[i]=latnum;
        lonarray[i]=lonnum;
        valarray[i]=valnum;
    
    
        i=i+1;  
    }
    fclose(fp);

    sprintf(inpFnameb,"100_%s.txt2",datetime);
    printf("%s\n",inpFnameb);

    if ((fpb=fopen(inpFnameb,"rt"))==NULL)
    {
        printf("\nERROR: Cannot open/read input file [%s]\n\n",inpFnameb);
    }

    i=0;
    while(i < 3185 && fgets(buf,sizeof buf, fpb))
    {
        sscanf(buf,"%f %f %f\n",&latnumb,&lonnumb,&valnumb);
    
        latarrayb[i]=latnumb;
        lonarrayb[i]=lonnumb;
        valarrayb[i]=valnumb;
    
        i=i+1;  
    }
    fclose(fpb);

    sprintf(inpFnamec,"100_%s.txt3",datetime);
    printf("%s\n",inpFnamec);

    if ((fpc=fopen(inpFnamec,"rt"))==NULL)
    {
        printf("\nERROR: Cannot open/read input file [%s]\n\n",inpFnamec);
    }

    i=0;
    while(i < 3185 && fgets(buf,sizeof buf, fpb))
    {
        sscanf(buf,"%f %f %f\n",&latnumc,&lonnumc,&valnumc);
    
        latarrayc[i]=latnumc;
        lonarrayc[i]=lonnumc;
        valarrayc[i]=valnumc;
    
        i=i+1;  
    }
    fclose(fpc);


}

Unfortunately when running this code I am getting a segmenation fault.不幸的是,在运行此代码时,我遇到了分段错误。 It I take the third set of arrays out (arrayc), I do not get this segmentation fault so there must be a memory issue possibly.我取出第三组 arrays (arrayc),我没有得到这个分段错误,所以可能有 memory 问题。 How do I tweak this program so as not to get a segmentation fault and be able to achieve my desired result of getting a latitude, longitude and value array for each file for a total of 9 arrays?如何调整这个程序,以免出现分段错误,并能够实现我想要的结果,即为每个文件获取纬度、经度和值数组,总共 9 个 arrays?

Instead of creating 3 sets of arrays and combining them later, you can create one set of arrays large enough for all the files, read the data directly in to the large array.无需创建 3 组 arrays 并在以后组合它们,您可以创建一组足够大的 arrays 用于所有文件,直接将数据读入大数组。

All the files appear to have the same format, so use a function to repeat the same read operation.所有文件似乎都具有相同的格式,因此使用 function 重复相同的读取操作。 There is no need for 3 sets of temporary variables for each file.每个文件不需要 3 组临时变量。

Open the files with "r" format.打开"r"格式的文件。 Valid open read modes are "r" or "rb" for binary.有效的开放读取模式是二进制的"r""rb"

Add error checks for malloc , sscanf , argc .添加对mallocsscanfargc的错误检查。 Remove variables which are not used.删除不使用的变量。

You can improve the code by using realloc , this way you don't have to know the size of the array ahead of time.您可以使用realloc改进代码,这样您就不必提前知道数组的大小。

void readfile(const char *filename, int *iptr, int count,
    float* latarray, float* lonarray, float* valarray)
{
    int i = *iptr;
    char buf[8000];
    FILE* fp = fopen(filename, "r");
    if (!fp) { perror(filename); exit(1); }
    while (i < count && fgets(buf, sizeof buf, fp))
    {
        if (sscanf(buf, "%f %f %f\n", 
            &latarray[i], &lonarray[i], &valarray[i]) != 3)
            continue;
        i++;
    }
    *iptr = i;
    fclose(fp);
}

int main(int argc, char* argv[])
{
    if (argc < 2) return 0;
    char datetime[10];
    sprintf(datetime, "%s", argv[1]);

    char filename[100];

    int i = 0;
    int count = 1038240 + 3185 + 5265;
    float* latarray = malloc(sizeof(float) * count);
    float* lonarray = malloc(sizeof(float) * count);
    float* valarray = malloc(sizeof(float) * count);
    if (!latarray) return 0;
    if (!lonarray) return 0;
    if (!valarray) return 0;    

    sprintf(filename, "100_%s.txt1", datetime);
    readfile(filename, &i, count, latarray, lonarray, valarray);

    sprintf(filename, "100_%s.txt2", datetime);
    readfile(filename, &i, count, latarray, lonarray, valarray);

    sprintf(filename, "100_%s.txt3", datetime);
    readfile(filename, &i, count, latarray, lonarray, valarray);

    printf("Total lines read: %d\n", i);

    free(latarray);
    free(lonarray);
    free(valarray);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM