简体   繁体   English

使用 c 编程将文件中的行放入二维数组

[英]putting lines from a file into a two dimensional array using c programming

I have a file with several lines that look like this:我有一个包含几行的文件,如下所示:

-180 -90 56 67
-179 -90 56 99
-178 -90 56 99
...
180 -90 45 45
-180 -89 45 45

The first two columns of each line are longitude and latitude values respectively.每行的前两列分别是经度和纬度值。 Now what I want to do is put these lines in a two dimensional array called filearrray[i][j] where i is longitude indices and j is latitude indices.现在我想要做的是将这些行放在一个名为 filearray[i][j] 的二维数组中,其中 i 是经度索引,j 是纬度索引。 Below is my attempt to do this:以下是我的尝试:

int main(int argc, char *argv[])
{
    FILE    *fp=NULL;
    FILE    FILEOUT[150];
    char    inpFname[81],FILEOUT[150];
    char    buf[8000];
    char*   filearray = malloc(sizeof(float) * (159*360));
    int     i,j;
    int     date = atoi(argv[1]);   


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

    i=0;
    j=0;
    while(fgets(buf,sizeof buf, fp))
    {
        if( i >= 359 )
        {
            i=0;
            j=j+1;
        }
        sprintf(filearray[i][j],"%s",buf);
        i=i+1;
        

    }
}

However upon compiling the program I get the following error:但是,在编译程序时,我收到以下错误:

  translate_ww3file.c:34:23: error: subscripted value is neither array nor pointer nor     vector
       sprintf(filearray[i][j],"%s",buf);

How do I tweak this code so I can get the desired results?如何调整此代码以便获得所需的结果?

The line线

char*   filearray = malloc(sizeof(float) * (159*360));

is wrong, for several reasons:是错误的,有几个原因:

In that line, you are allocating space for 159*360 elements of type float .在该行中,您为 159*360 类型的float元素分配空间。 However, you are assigning that space to a char * .但是,您将该空间分配给char * This does not make sense.这根本不符合逻辑。 If every element in the 2D array should be a float , then you should write this instead:如果 2D 数组中的每个元素都应该是float ,那么您应该改为:

float (*filearray)[159] = malloc( sizeof(float) * (360*159) );

In the line above, it is necessary to write float (*filearray)[159] instead of float *filearray[159] , because the latter declares an array of 159 elements in which every element is a float* , whereas the former declares one pointer to an array of 159 elements in which every element is a float .在上面的行中,有必要写float (*filearray)[159]而不是float *filearray[159] ,因为后者声明了一个包含 159 个元素的数组,其中每个元素都是一个float* ,而前者声明了一个指向一个包含 159 个元素的数组的指针,其中每个元素都是一个float

Also, the number 159 seems wrong.此外,数字159似乎是错误的。 If latitudes are numbers between -90 and +90 inclusive, then there are 181 possible numbers, so you should be using the number 181 instead of 159 .如果纬度是介于-90+90之间的数字,则有181个可能的数字,因此您应该使用数字181而不是159 For the same reason, you should probably be using the number 361 instead of 360 for the longitude.出于同样的原因,您可能应该使用数字361而不是360作为经度。

However, later in your program, you use the line:但是,稍后在您的程序中,您使用以下行:

sprintf(filearray[i][j],"%s",buf);

This implies that you don't want every element of the 2D array to be a float , because you can't sprintf to a float .这意味着您不希望 2D 数组的每个元素都是float ,因为您不能sprintffloat Instead, it appears that you want every element to be a string.相反,您似乎希望每个元素都是一个字符串。

If you want every element of the 2D array to be a string, then you must allocate sufficient space for every string.如果您希望 2D 数组的每个元素都是字符串,那么您必须为每个字符串分配足够的空间。 Simply allocating sizeof(float) bytes for every string will not be sufficient.简单地为每个字符串分配sizeof(float)字节是不够的。

Since the two numbers that follow the longitude and latitude in every line seem to be integers, it may be appropriate to make every element of the 2D array store these two numbers as a pair of int , instead of using strings.由于每行中经度和纬度后面的两个数字似乎是整数,因此可能适合让二维数组的每个元素将这两个数字存储为一对int ,而不是使用字符串。 You could define the following type:您可以定义以下类型:

struct pair
{
    int number1;
    int number2;
};

You could then define your 2D array like this:然后你可以像这样定义你的二维数组:

struct pair (*filearray)[181] = malloc( sizeof(struct pair) * (361*181) );

It is also worth noting that it is good programming practice to call free for every malloc call when you no longer need the memory (in this case at the end of your program).还值得注意的是,当您不再需要 memory 时(在这种情况下,在程序结束时),为每个malloc调用free调用是一种很好的编程习惯。

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

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