gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:47:49 25 4
gpt4 key购买 nike

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

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

可以找到源文件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;
}

最佳答案

更新 #2: 发现了真正的问题:输入循环的交替行没有被数据填充。 偶数行的读取将根据列数完成,但不会吸收其对应的换行符。以下奇数 行将看到一个立即 换行符并终止而不存储任何数据


我清理了您的代码并添加了输出打印 [请原谅不必要的样式清理]。值得注意的是,如果行很短或遇到过早的 EOF,“PEAR SHAPED”循环的脆弱性。我还修复了“偶数/奇数”错误。

#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

关于c - 执行文件 I/O 时出现意外的零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com