执行文件I / O时出现意外的零

时间:2015-12-27 23:06:44

标签: c file-io multidimensional-array

我有一个程序,我想从文本源文件中的行中获取单个字符,并将它们存储在二维数组中(我称之为numbers[][N])。在我的源文件中的所有字符都存储在numbers后,我将Nnumbers列加到另一个名为temp[N]的数组中。

问题是,在我确定源文本中的最长行和总行数后,我重置了我的文件指针并使用fgetc()一次拉出一个字符并存储它们的值在numbers的元素中。但是在这个实现中,我得到了存储在0中的奇数字符串(整数文字numbers),其中应该存在非零值。为什么呢?

可以找到源文件sumSource.txt的文本 here

注意:我已经设法用不同的实现来引发适当的行为,但我想知道为什么这个代码给了我奇怪的零字符串。

#include <stdio.h>

// define a sourcefile here
#define SOURCE_FILE "/home/demiurge/play/euler/sumSource.txt"

int main(void)
{
    // generic indices
    int i, j, k;

    // open a stream to sourcefile here
    FILE* _Fsource = fopen( SOURCE_FILE, "r" );

    // Determine the longest number of digits in any line
    // _Fsource and the total number of terms in the sum.
    int ROWS = 0;
    int COLS = 0;

    // Recent char from sourcefile
    int c;

    do { // I'm doing this do/while statement so that 'count' is automatic

        int count = 0;
        while ( ( c = fgetc( _Fsource ) ) != EOF ) {
            if( c == '\n' ) {
                ROWS++;
                if( COLS < count ) {
                    COLS = count;
                }
                count = 0;
            }
            else {
                count++;
            }
        }
    } while ( c != EOF );

    // Reset position of _Fsource to start of
    // sourcefile
    fseek( _Fsource, 0L, SEEK_SET );

    // create a storage mechanism for
    // the partial sum of the numbers
    // in sourcefile
    int numbers[ROWS][COLS];
    int temp[COLS];

    // set every element in temp to zero
    for ( i = 0; i < COLS; i++ ) {
        temp[i] = 0;
    }

// THINGS GO PEAR-SHAPED HERE; WHY?
    for ( i = 0; i < ROWS; i++ ) {
        for ( j = 0; j < COLS && \
            ( c = fgetc( _Fsource ) ) != '\n'; j++ ) {

            numbers[i][j] = ( c - '0' );
        }
    }

    for ( i = 0; i < ROWS; i++ ) {
        for ( j = 0; j < COLS; j++ ) {
            temp[j] += numbers[i][j];
        }
    }

    return 0;
}

4 个答案:

答案 0 :(得分:1)

更新#2:发现真实问题:输入循环的备用行正在填充数据。读取偶数行将在列数上完成,但吸收其相应的换行符。以下奇数行将看到立即换行符并终止而不存储任何数据

我清理了你的代码并添加了输出打印[请原谅无偿的样式清理]。值得注意的是,#PE; PEAR SHAPED&#34;如果线条很短或遇到过早的EOF,则循环。我还为&#34;偶数/奇数&#34;提供了解决方案。错误。

#include <stdio.h>

// define a sourcefile here
//#define SOURCE_FILE "/home/demiurge/play/euler/sumSource.txt"
#define SOURCE_FILE "sumSource.txt"

int
main(void)
{
    // generic indices
    int icol;
    int irow;

    int nlflg;

    // open a stream to sourcefile here
    FILE *_Fsource = fopen(SOURCE_FILE, "r");

    // Determine the longest number of digits in any line
    // _Fsource and the total number of terms in the sum.
    int ROWS = 0;
    int COLS = 0;

    // Recent char from sourcefile
    int c;

    // I'm doing this do/while statement so that 'count' is automatic
    do {
        int count = 0;

        while (1) {
            c = fgetc(_Fsource);
            if (c == EOF)
                break;

            if (c == '\n') {
                ROWS++;
                if (COLS < count)
                    COLS = count;
                count = 0;
            }
            else
                count++;
        }
    } while (0);

    printf("ROWS=%d COLS=%d\n",ROWS,COLS);

    // Reset position of _Fsource to start of
    // sourcefile
    fseek(_Fsource, 0L, SEEK_SET);

    // create a storage mechanism for
    // the partial sum of the numbers
    // in sourcefile
    int numbers[ROWS][COLS];
    int temp[COLS];

    // set every element in temp to zero
    for (icol = 0; icol < COLS; icol++)
        temp[icol] = 0;

    // in case of some lines are "short"
    for (irow = 0; irow < ROWS; irow++) {
        for (icol = 0; icol < COLS; icol++)
            numbers[irow][icol] = 0;
    }

    // THINGS GO PEAR-SHAPED HERE; WHY?
    c = EOF;
    for (irow = 0; irow < ROWS; irow++) {
        nlflg = 0;

        for (icol = 0; icol < COLS; icol++) {
            c = fgetc(_Fsource);
            if (c == EOF)
                break;

            // NOTE: in original code, this would cause alternate loop to
            // terminate due to "off-by-one" error
            // that is, even loops would fail to absorb their newlines and
            // cause odd loops to terminate without storing anything
            if (c == '\n') {
                nlflg = 1;
                break;
            }

            if (irow < 5)
                printf("DEBUG/READ: %d,%d c=%2.2X/%d\n",irow,icol,c,c - '0');
            numbers[irow][icol] = (c - '0');
        }

        if (c == EOF)
            break;

        // NOTE: without this, every alternate row would be skipped
        if (! nlflg) {
            while (1) {
                c = fgetc(_Fsource);
                if (c == EOF)
                    break;
                if (c == '\n')
                    break;
            }
            if (c == EOF)
                break;
        }
    }

    for (irow = 0; irow < ROWS; irow++) {
        for (icol = 0; icol < COLS; icol++)
            temp[icol] += numbers[irow][icol];
    }

    for (icol = 0; icol < COLS; icol++)
        printf("%d: %d\n",icol,temp[icol]);

    return 0;
}

我运行了原始代码[带输出语句]和我的。这就是区别:

--- /tmp/orig   2015-12-27 17:45:57.700757183 -0800
+++ /tmp/fixed  2015-12-27 17:45:14.236833644 -0800
@@ -49,153 +49,253 @@
 DEBUG/READ: 0,47 c=32/2
 DEBUG/READ: 0,48 c=35/5
 DEBUG/READ: 0,49 c=30/0
-DEBUG/READ: 2,0 c=34/4
-DEBUG/READ: 2,1 c=36/6
+DEBUG/READ: 1,0 c=34/4
+DEBUG/READ: 1,1 c=36/6
+DEBUG/READ: 1,2 c=33/3
+DEBUG/READ: 1,3 c=37/7
+DEBUG/READ: 1,4 c=36/6
+DEBUG/READ: 1,5 c=39/9
+DEBUG/READ: 1,6 c=33/3
+DEBUG/READ: 1,7 c=37/7
+DEBUG/READ: 1,8 c=36/6
+DEBUG/READ: 1,9 c=37/7
+DEBUG/READ: 1,10 c=37/7
+DEBUG/READ: 1,11 c=34/4
+DEBUG/READ: 1,12 c=39/9
+DEBUG/READ: 1,13 c=30/0
+DEBUG/READ: 1,14 c=30/0
+DEBUG/READ: 1,15 c=30/0
+DEBUG/READ: 1,16 c=39/9
+DEBUG/READ: 1,17 c=37/7
+DEBUG/READ: 1,18 c=31/1
+DEBUG/READ: 1,19 c=32/2
+DEBUG/READ: 1,20 c=36/6
+DEBUG/READ: 1,21 c=34/4
+DEBUG/READ: 1,22 c=38/8
+DEBUG/READ: 1,23 c=31/1
+DEBUG/READ: 1,24 c=32/2
+DEBUG/READ: 1,25 c=34/4
+DEBUG/READ: 1,26 c=38/8
+DEBUG/READ: 1,27 c=39/9
+DEBUG/READ: 1,28 c=36/6
+DEBUG/READ: 1,29 c=39/9
+DEBUG/READ: 1,30 c=37/7
+DEBUG/READ: 1,31 c=30/0
+DEBUG/READ: 1,32 c=30/0
+DEBUG/READ: 1,33 c=37/7
+DEBUG/READ: 1,34 c=38/8
+DEBUG/READ: 1,35 c=30/0
+DEBUG/READ: 1,36 c=35/5
+DEBUG/READ: 1,37 c=30/0
+DEBUG/READ: 1,38 c=34/4
+DEBUG/READ: 1,39 c=31/1
+DEBUG/READ: 1,40 c=37/7
+DEBUG/READ: 1,41 c=30/0
+DEBUG/READ: 1,42 c=31/1
+DEBUG/READ: 1,43 c=38/8
+DEBUG/READ: 1,44 c=32/2
+DEBUG/READ: 1,45 c=36/6
+DEBUG/READ: 1,46 c=30/0
+DEBUG/READ: 1,47 c=35/5
+DEBUG/READ: 1,48 c=33/3
+DEBUG/READ: 1,49 c=38/8
+DEBUG/READ: 2,0 c=37/7
+DEBUG/READ: 2,1 c=34/4
 DEBUG/READ: 2,2 c=33/3
-DEBUG/READ: 2,3 c=37/7
-DEBUG/READ: 2,4 c=36/6
+DEBUG/READ: 2,3 c=32/2
+DEBUG/READ: 2,4 c=34/4
 DEBUG/READ: 2,5 c=39/9
-DEBUG/READ: 2,6 c=33/3
-DEBUG/READ: 2,7 c=37/7
-DEBUG/READ: 2,8 c=36/6
-DEBUG/READ: 2,9 c=37/7
-DEBUG/READ: 2,10 c=37/7
-DEBUG/READ: 2,11 c=34/4
-DEBUG/READ: 2,12 c=39/9
-DEBUG/READ: 2,13 c=30/0
-DEBUG/READ: 2,14 c=30/0
-DEBUG/READ: 2,15 c=30/0
-DEBUG/READ: 2,16 c=39/9
-DEBUG/READ: 2,17 c=37/7
-DEBUG/READ: 2,18 c=31/1
-DEBUG/READ: 2,19 c=32/2
-DEBUG/READ: 2,20 c=36/6
-DEBUG/READ: 2,21 c=34/4
-DEBUG/READ: 2,22 c=38/8
-DEBUG/READ: 2,23 c=31/1
-DEBUG/READ: 2,24 c=32/2
-DEBUG/READ: 2,25 c=34/4
-DEBUG/READ: 2,26 c=38/8
-DEBUG/READ: 2,27 c=39/9
-DEBUG/READ: 2,28 c=36/6
-DEBUG/READ: 2,29 c=39/9
-DEBUG/READ: 2,30 c=37/7
-DEBUG/READ: 2,31 c=30/0
+DEBUG/READ: 2,6 c=38/8
+DEBUG/READ: 2,7 c=36/6
+DEBUG/READ: 2,8 c=31/1
+DEBUG/READ: 2,9 c=39/9
+DEBUG/READ: 2,10 c=39/9
+DEBUG/READ: 2,11 c=35/5
+DEBUG/READ: 2,12 c=32/2
+DEBUG/READ: 2,13 c=34/4
+DEBUG/READ: 2,14 c=37/7
+DEBUG/READ: 2,15 c=34/4
+DEBUG/READ: 2,16 c=31/1
+DEBUG/READ: 2,17 c=30/0
+DEBUG/READ: 2,18 c=35/5
+DEBUG/READ: 2,19 c=39/9
+DEBUG/READ: 2,20 c=34/4
+DEBUG/READ: 2,21 c=37/7
+DEBUG/READ: 2,22 c=34/4
+DEBUG/READ: 2,23 c=32/2
+DEBUG/READ: 2,24 c=33/3
+DEBUG/READ: 2,25 c=33/3
+DEBUG/READ: 2,26 c=33/3
+DEBUG/READ: 2,27 c=30/0
+DEBUG/READ: 2,28 c=39/9
+DEBUG/READ: 2,29 c=35/5
+DEBUG/READ: 2,30 c=31/1
+DEBUG/READ: 2,31 c=33/3
 DEBUG/READ: 2,32 c=30/0
-DEBUG/READ: 2,33 c=37/7
+DEBUG/READ: 2,33 c=35/5
 DEBUG/READ: 2,34 c=38/8
-DEBUG/READ: 2,35 c=30/0
-DEBUG/READ: 2,36 c=35/5
-DEBUG/READ: 2,37 c=30/0
-DEBUG/READ: 2,38 c=34/4
-DEBUG/READ: 2,39 c=31/1
-DEBUG/READ: 2,40 c=37/7
-DEBUG/READ: 2,41 c=30/0
+DEBUG/READ: 2,35 c=31/1
+DEBUG/READ: 2,36 c=32/2
+DEBUG/READ: 2,37 c=33/3
+DEBUG/READ: 2,38 c=37/7
+DEBUG/READ: 2,39 c=32/2
+DEBUG/READ: 2,40 c=36/6
+DEBUG/READ: 2,41 c=36/6
 DEBUG/READ: 2,42 c=31/1
-DEBUG/READ: 2,43 c=38/8
-DEBUG/READ: 2,44 c=32/2
-DEBUG/READ: 2,45 c=36/6
-DEBUG/READ: 2,46 c=30/0
-DEBUG/READ: 2,47 c=35/5
-DEBUG/READ: 2,48 c=33/3
-DEBUG/READ: 2,49 c=38/8
-DEBUG/READ: 4,0 c=37/7
-DEBUG/READ: 4,1 c=34/4
-DEBUG/READ: 4,2 c=33/3
-DEBUG/READ: 4,3 c=32/2
-DEBUG/READ: 4,4 c=34/4
-DEBUG/READ: 4,5 c=39/9
+DEBUG/READ: 2,43 c=37/7
+DEBUG/READ: 2,44 c=33/3
+DEBUG/READ: 2,45 c=30/0
+DEBUG/READ: 2,46 c=39/9
+DEBUG/READ: 2,47 c=36/6
+DEBUG/READ: 2,48 c=32/2
+DEBUG/READ: 2,49 c=39/9
+DEBUG/READ: 3,0 c=39/9
+DEBUG/READ: 3,1 c=31/1
+DEBUG/READ: 3,2 c=39/9
+DEBUG/READ: 3,3 c=34/4
+DEBUG/READ: 3,4 c=32/2
+DEBUG/READ: 3,5 c=32/2
+DEBUG/READ: 3,6 c=31/1
+DEBUG/READ: 3,7 c=33/3
+DEBUG/READ: 3,8 c=33/3
+DEBUG/READ: 3,9 c=36/6
+DEBUG/READ: 3,10 c=33/3
+DEBUG/READ: 3,11 c=35/5
+DEBUG/READ: 3,12 c=37/7
+DEBUG/READ: 3,13 c=34/4
+DEBUG/READ: 3,14 c=31/1
+DEBUG/READ: 3,15 c=36/6
+DEBUG/READ: 3,16 c=31/1
+DEBUG/READ: 3,17 c=35/5
+DEBUG/READ: 3,18 c=37/7
+DEBUG/READ: 3,19 c=32/2
+DEBUG/READ: 3,20 c=35/5
+DEBUG/READ: 3,21 c=32/2
+DEBUG/READ: 3,22 c=32/2
+DEBUG/READ: 3,23 c=34/4
+DEBUG/READ: 3,24 c=33/3
+DEBUG/READ: 3,25 c=30/0
+DEBUG/READ: 3,26 c=35/5
+DEBUG/READ: 3,27 c=36/6
+DEBUG/READ: 3,28 c=33/3
+DEBUG/READ: 3,29 c=33/3
+DEBUG/READ: 3,30 c=30/0
+DEBUG/READ: 3,31 c=31/1
+DEBUG/READ: 3,32 c=38/8
+DEBUG/READ: 3,33 c=31/1
+DEBUG/READ: 3,34 c=31/1
+DEBUG/READ: 3,35 c=30/0
+DEBUG/READ: 3,36 c=37/7
+DEBUG/READ: 3,37 c=32/2
+DEBUG/READ: 3,38 c=34/4
+DEBUG/READ: 3,39 c=30/0
+DEBUG/READ: 3,40 c=36/6
+DEBUG/READ: 3,41 c=31/1
+DEBUG/READ: 3,42 c=35/5
+DEBUG/READ: 3,43 c=34/4
+DEBUG/READ: 3,44 c=39/9
+DEBUG/READ: 3,45 c=30/0
+DEBUG/READ: 3,46 c=38/8
+DEBUG/READ: 3,47 c=32/2
+DEBUG/READ: 3,48 c=35/5
+DEBUG/READ: 3,49 c=30/0
+DEBUG/READ: 4,0 c=32/2
+DEBUG/READ: 4,1 c=33/3
+DEBUG/READ: 4,2 c=30/0
+DEBUG/READ: 4,3 c=36/6
+DEBUG/READ: 4,4 c=37/7
+DEBUG/READ: 4,5 c=35/5
 DEBUG/READ: 4,6 c=38/8
-DEBUG/READ: 4,7 c=36/6
-DEBUG/READ: 4,8 c=31/1
-DEBUG/READ: 4,9 c=39/9
-DEBUG/READ: 4,10 c=39/9
+DEBUG/READ: 4,7 c=38/8
+DEBUG/READ: 4,8 c=32/2
+DEBUG/READ: 4,9 c=30/0
+DEBUG/READ: 4,10 c=37/7
 DEBUG/READ: 4,11 c=35/5
-DEBUG/READ: 4,12 c=32/2
-DEBUG/READ: 4,13 c=34/4
-DEBUG/READ: 4,14 c=37/7
+DEBUG/READ: 4,12 c=33/3
+DEBUG/READ: 4,13 c=39/9
+DEBUG/READ: 4,14 c=33/3
 DEBUG/READ: 4,15 c=34/4
-DEBUG/READ: 4,16 c=31/1
-DEBUG/READ: 4,17 c=30/0
-DEBUG/READ: 4,18 c=35/5
-DEBUG/READ: 4,19 c=39/9
-DEBUG/READ: 4,20 c=34/4
+DEBUG/READ: 4,16 c=36/6
+DEBUG/READ: 4,17 c=31/1
+DEBUG/READ: 4,18 c=37/7
+DEBUG/READ: 4,19 c=31/1
+DEBUG/READ: 4,20 c=31/1
 DEBUG/READ: 4,21 c=37/7
-DEBUG/READ: 4,22 c=34/4
-DEBUG/READ: 4,23 c=32/2
-DEBUG/READ: 4,24 c=33/3
-DEBUG/READ: 4,25 c=33/3
+DEBUG/READ: 4,22 c=31/1
+DEBUG/READ: 4,23 c=39/9
+DEBUG/READ: 4,24 c=38/8
+DEBUG/READ: 4,25 c=30/0
 DEBUG/READ: 4,26 c=33/3
-DEBUG/READ: 4,27 c=30/0
-DEBUG/READ: 4,28 c=39/9
-DEBUG/READ: 4,29 c=35/5
-DEBUG/READ: 4,30 c=31/1
-DEBUG/READ: 4,31 c=33/3
+DEBUG/READ: 4,27 c=31/1
+DEBUG/READ: 4,28 c=30/0
+DEBUG/READ: 4,29 c=34/4
+DEBUG/READ: 4,30 c=32/2
+DEBUG/READ: 4,31 c=31/1
 DEBUG/READ: 4,32 c=30/0
-DEBUG/READ: 4,33 c=35/5
-DEBUG/READ: 4,34 c=38/8
-DEBUG/READ: 4,35 c=31/1
-DEBUG/READ: 4,36 c=32/2
+DEBUG/READ: 4,33 c=34/4
+DEBUG/READ: 4,34 c=37/7
+DEBUG/READ: 4,35 c=35/5
+DEBUG/READ: 4,36 c=31/1
 DEBUG/READ: 4,37 c=33/3
 DEBUG/READ: 4,38 c=37/7
-DEBUG/READ: 4,39 c=32/2
-DEBUG/READ: 4,40 c=36/6
-DEBUG/READ: 4,41 c=36/6
-DEBUG/READ: 4,42 c=31/1
-DEBUG/READ: 4,43 c=37/7
-DEBUG/READ: 4,44 c=33/3
-DEBUG/READ: 4,45 c=30/0
-DEBUG/READ: 4,46 c=39/9
+DEBUG/READ: 4,39 c=37/7
+DEBUG/READ: 4,40 c=38/8
+DEBUG/READ: 4,41 c=30/0
+DEBUG/READ: 4,42 c=36/6
+DEBUG/READ: 4,43 c=33/3
+DEBUG/READ: 4,44 c=32/2
+DEBUG/READ: 4,45 c=34/4
+DEBUG/READ: 4,46 c=36/6
 DEBUG/READ: 4,47 c=36/6
-DEBUG/READ: 4,48 c=32/2
-DEBUG/READ: 4,49 c=39/9
-0: 189301617
-1: 98463
-2: -536821826
-3: 65685
-4: 1149687807
-5: 33045
-6: -330672558
-7: 309
-8: 1149933361
-9: 495
-10: 715103802
-11: 33063
-12: -739952904
-13: 33225
-14: 1041326090
-15: 98512
-16: 723772574
-17: 65884
-18: -700583228
-19: 65755
-20: 1151592341
-21: 33082
-22: 635881077
-23: 98456
-24: 1670167446
-25: 65939
-26: 1352358526
-27: 32936
-28: -1507047068
-29: 131126
-30: 28632144
-31: 3670307
-32: -1457728577
-33: 5013884
-34: 1632187
-35: 202
-36: 890045461
-37: 354
-38: -1232222461
-39: 349
-40: 518640906
-41: 33102
-42: -538593328
-43: 65655
-44: 1803206122
-45: 33018
-46: 429048077
-47: 335
-48: 1669065150
-49: 65900
+DEBUG/READ: 4,48 c=37/7
+DEBUG/READ: 4,49 c=36/6
+0: 506
+1: 428
+2: 443
+3: 452
+4: 503
+5: 480
+6: 474
+7: 441
+8: 446
+9: 432
+10: 422
+11: 440
+12: 436
+13: 469
+14: 426
+15: 454
+16: 447
+17: 453
+18: 453
+19: 419
+20: 486
+21: 392
+22: 484
+23: 394
+24: 482
+25: 476
+26: 462
+27: 409
+28: 487
+29: 512
+30: 427
+31: 464
+32: 466
+33: 487
+34: 454
+35: 427
+36: 458
+37: 443
+38: 455
+39: 470
+40: 410
+41: 449
+42: 441
+43: 468
+44: 486
+45: 478
+46: 462
+47: 460
+48: 425
+49: 422

答案 1 :(得分:1)

您在numbers上使用未分配的内存。您必须使用malloc或使用此:

    for ( i = 0; i < ROWS; i++ ) {
        for ( j = 0; j < COLS ; j++ ) {
          numbers[i][j] = 0;
        }
    }

这有助于我的gcc。您的完整代码:

#include <stdio.h>

// define a sourcefile here
#define SOURCE_FILE "/tmp/src.txt"

int main(void)
{
    // generic indices
    int i, j, k;

    // open a stream to sourcefile here
    FILE* _Fsource = fopen( SOURCE_FILE, "r" );

    // Determine the longest number of digits in any line
    // _Fsource and the total number of terms in the sum.
    int ROWS = 0;
    int COLS = 0;

    // Recent char from sourcefile
    int c;

    do { // I'm doing this do/while statement so that 'count' is automatic

        int count = 0;
        while ( ( c = fgetc( _Fsource ) ) != EOF ) {
            if( c == '\n' ) {
                ROWS++;
                if( COLS < count ) {
                    COLS = count;
                }
                count = 0;
            }
            else {
                count++;
            }
        }
    } while ( c != EOF );

    // Reset position of _Fsource to start of
    // sourcefile
    fseek( _Fsource, 0L, SEEK_SET );

    // create a storage mechanism for
    // the partial sum of the numbers
    // in sourcefile
    int numbers[ROWS][COLS];
    int temp[COLS];

    // set every element in temp to zero
    for ( i = 0; i < COLS; i++ ) {
        temp[i] = 0;
    }
    for ( i = 0; i < ROWS; i++ ) {
        for ( j = 0; j < COLS ; j++ ) {
          numbers[i][j] = 0;
        }
    }
// THINGS GO PEAR-SHAPED HERE; WHY?
    for ( i = 0; i < ROWS; i++ ) {
        for ( j = 0; j < COLS && \
            ( c = fgetc( _Fsource ) ) != '\n'; j++ ) {

            numbers[i][j] = ( c - '0' );
        }
    }

    for ( i = 0; i < 3; i++ ) {
        for ( j = 0; j < COLS; j++ ) {
            temp[j] += numbers[i][j];
        }
    }

    return 0;
}

答案 2 :(得分:1)

您正在阅读时跳过行,并且您的矩阵未初始化。我得到正确的值重写这样的循环(源文件中的所有行具有相同的长度):

for ( i = 0; i < ROWS; i++ ) {
    j = 0;
    while( (c = fgetc( _Fsource ) ) >= '0'  &&  c <= '9') {
        numbers[i][j] = ( c - '0' );
        j++;
    }
}

正如我在评论中提到的,我还想向您介绍来自源文件的可能错误,其中包含不同长度的行。你怎么看待丢失的数字?例如,拥有:

1234
123

结果是2464=1234+1230)或1357=1234+123)?

在第一种情况下,您应该正确初始化所有元素:

for ( i = 0; i < ROWS; i++ ) {
    for ( j = 0; (c = fgetc( source ) ) >='0' && c<='9'; j++ ) {
        numbers[i][j] = ( c - '0' );            
    }
    while ( j < COLS ) {
        numbers[i][j] = 0;
        j++;
    }
}

对于第二种情况,您可以移动元素:

for ( i = 0; i < ROWS; i++ ) {
    for ( j = 0; (c = fgetc( source ) ) >='0' && c<='9'; j++ ) {
        numbers[i][j] = ( c - '0' );            
    }
    if ( j == 0 ) {                         // bad input
        for ( int k = 0; k < COLS; k++ ) {
            numbers[i][j] = 0;
        }
    } else if ( j < COLS ) {
        int d = COLS - j;
        int k;
        for ( k = COLS-1; k >= d; k-- ) {
            numbers[i][k] = numbers[i][k-d];
        }
        while ( k >= 0 ) {
        numbers[i][k] = 0;
        k--;
        }
    }
}

答案 3 :(得分:0)

首先,如果要执行列总和,则需要将所有矩阵初始化为0.

然后请记住,源文件的每一行都以“\ r \ n”结尾,而不只是“\ n”。 您可以像这样调整代码:

for ( i = 0; i < ROWS; i++ ) {
    for ( j = 0; j < COLS && \
        ( c = fgetc( _Fsource ) ) != '\r'; j++ ) {//stops at \r
        numbers[i][j] = ( c - '0' );
    }
    c = fgetc(_Fsource);//read \n
}