如何将数据文件输入到二维数组中 [英] How to input a data file into a 2D array

查看:610
本文介绍了如何将数据文件输入到二维数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

信息内读程当亦读范范中内的数范信息信息区亦读信范亦范范范范范范范范范范内范范范范范范辛范范范辛范范信信信范辛辛亦信范辛我所知道的是,双打将被空格和换行符隔开,例如:

$ p $ 2.04 3.0 4.0
5.0 6.1 7.0
8.0 9.03 10.0

我不知道会有多少个数字,所以我需要一个可扩展的解析函数。到目前为止,我有:

$ pre code $ FILE * fp;
char ch;
fp = fopen(matrix.dat,r);
if(fp == NULL)
{
puts(Can not open source file);
}

/ *计算行数* /
int lines = 1;
char c; ((c = fgetc(fp))!= EOF)

{
if(c =='\\\
')
lines ++;
}
printf(lines =%i\\\
,lines);

/ *重新打开文件重置指针* /
fclose(fp);
fp = fopen(matrix.dat,r);
double m [lines] [lines];

/ *将文件加载到数组* /
int i,j; (j = 0; j <行; j ++)
{
fscanf(fp,
)为(i = 0; i }
fscanf(fp,\\\
,NULL); (i = 0; i {
for(j = 0; j {
printf(%lf,m [i] [j]);
}
printf(\\\
);


$ / code $ / pre

我的这个方法的问题是


  1. 它假设行数等于每行双打的数量,这可能是不正确的。 / li>

  2. 文件中任何缺少的双精度将导致程序将错误的矩阵加载到内存中(其余值偏移1)。

  3. 我正在将文件加载到内存中两次以计算行数。

是否有更合理的方法将这些数据输入到二维数组中?我需要确保输入矩阵是正方形的,而且我的程序可以处理任何顺序的矩形矩阵。我对C很陌生,所以示例和命名函数将会非常感谢:)(对于不友好的缩进来说)



**对不起,想要一些错误处理,即确保输入不被接受,除非有nxn双精度。

解决方案

m好奇你的意思是按点(1)...如果矩阵是平方的,那么行数是否总是等于每行的双数?如果没有,那么我想你可以通过文件查看具有最大值的行。



以下是修改后的代码:

  / *计算行数* / 
int lines = 1;
int maxValues = 0;
int count = 0;
char垃圾; ($ j




$(b)$($) = count){
maxValues = ++ count;
}
count = 0;
}
else if(junk == 0x20){
count ++;




$ b $ p
$ p要解决点(2),也许尝试这个修改:

  / *将文件加载到数组中* / 
int i,j;
for(i = 0; i j = 0;
do {
fscanf(fp,%lf,&(m [i] [j]));
j ++; ((junk = fgetc(fp))!='\\\
');
}

$ / code $


最后对于点(3),我敢肯定你可以使用 rewind(fp); 将指针重置为文件开头。

I've got a file which I need to store in a 2-D array so I can perform matrix operations on it. All I know is that the doubles will be separated by spaces and line breaks, e.g:

2.04 3.0 4.0
5.0 6.1 7.0
8.0 9.03 10.0

I won't know how many numbers there will be, so I need a scalable parsing function. So far I have:

int main(int argc, char* argv[])
{
    FILE *fp;
    char ch;
    fp = fopen("matrix.dat","r");
    if ( fp == NULL )
    {
        puts ( "Cannot open source file");
    }

    /*count the number of lines*/
    int lines=1;
    char c;
    while((c = fgetc(fp)) != EOF)
    {
        if(c == '\n')
            lines++;
    }
    printf("lines= %i\n",lines);

    /*reopen the file to reset the pointer*/
    fclose(fp);
    fp = fopen("matrix.dat","r");
    double m[lines][lines];

    /*Load file into array*/
    int i,j;
    for(i=0;i<lines;i++)
    {
        for(j=0;j<lines;j++)
        {
            fscanf( fp, "%lf", &(m[i][j]));
        }
        fscanf(fp, "\n", NULL);
    }

    /*print out stored matrix*/
    for(i=0;i<lines;i++)
    {
        for(j=0;j<lines;j++)
        {
            printf("%lf ",m[i][j]);
        }
        printf("\n");
    }
}

My issues with this method are

  1. It makes the assumption that the number of rows equals the number of doubles per line, which may not be true.**

  2. Any missing doubles in the file will cause the program to load the wrong matrix into memory (remaining values offset by 1).

  3. I'm currently loading the file into memory twice to count the lines.

Is there a more sensible way to input this data into a 2D array? I need to ensure that the input matrix is square and that my program can handle a square matrix of any order. I'm quite new to C, so examples and naming functions will be much appreciated :) (Apologies for the dodgy indenting)

** Sorry for being so vague, I wanted some error handling, i.e making sure the input is not accepted unless there are exactly nxn doubles.

解决方案

Like Nicolas, I'm curious what you mean by point (1)...if the matrix is square, then won't the number of rows always equal the number of doubles per line? If not, then I suppose you could look through the file for the row with the most values.

Here's your code modified to do that:

/* count the number of lines */
int lines = 1;
int maxValues = 0;
int count = 0;
char junk;
while((junk == fgetc(fp)) != EOF) {
    if(junk == '\n') {
        lines++
        if(maxValues <= count) {
            maxValues = ++count;
        }
        count = 0;
    }
    else if(junk == 0x20) {
        count++;
    }
}

To address point (2), maybe try this modification:

/* load file into array */
int i, j;
for(i = 0; i < lines; i++) {
    j = 0;
    do {
        fscanf(fp, "%lf", &(m[i][j]));
        j++;
    } while((junk = fgetc(fp)) != '\n');
}

Finally for point (3), I'm pretty sure you can use rewind(fp); to reset the pointer to the beginning of the file.

这篇关于如何将数据文件输入到二维数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆