0

我正在学习 C 并通过编写一个从文本文件中读取整数并将它们存储到数组中的小程序来练习。但是,整数永远不会以某种方式存储,并且数组是空的。

 int readNumbers(int array[], char* fname) {                                                                                                                                                                                               
 78                                                                                                                                                                      
 79                                                                                                                                                                                             
 80     int numberRead = 0;                                                                                                                                                                                                                   
 81     FILE* fp;                                                                                                                                                                                                                             
 82     int ch;                                                                                                                                                                                                                               
 83     int i = 0;                                                                                                                                                                                                                            
 84                                                                                                                                                                                                                                           
 85                                                                                                                                                                                          
 86                                                                                                                                                                                                                                  
 87     fp = fopen(fname, "r");                                                                                                                                                                                                               
 88     // Test to see if the file was opened correctly                                                                                                                                                                                       
 89                                                                                                                                                                                                                                   
 90     if (fp == NULL) {                                                                                                                                                                                                                     
 91             printf("Error opening file\n");                                                                                                                                                                                               
 92             return;                                                                                                                                                                                                                       
 93     }                                                                                                                                                                                                                                     
 94     // Now read until end of file                                                                                                                                                                                                         
 95                                                                                                                                                                                                                                  
 96     while (ch = fgetc(fp) != EOF && isdigit(ch)) {                                                                                                                                                                                        
 97             array[i++] = ch;                                                                                                                                                                                                              
 98     }                                                                                                                                                                                                                                     
 99     if (ferror(fp)) {                                                                                                                                                                                                                     
100             return;                                                                                                                                                                                                                       
101     }                                                                                                                                                                                                                                     
102     // Close the file pointer                                                                                                                                                                                                             
103                                                                                                                                                                                                                                 
104     fclose(fp);                                                                                                                                                                                                                           
105                                                                                                                                                                                                                                           
106     // Return the number of items read                                                                                                                                                                                                    
107     return numberRead;                                                                                                                                                                          
108 }

文本文件将是这样的:

1 2 3 4 5 6 7 8 9

提前致谢。

我已经更新了代码。这几乎可以工作,但它可以解释诸如和之55类的字符。所以我的数组会有两个。555

 while ((ch =fgetc(fp)) != EOF) {                                                                                                                                                                                                      
 97             if (ch != ' ' && ch != '\n') {                                                                                                                                                                                                
 98                     array[counter] = ch - '0';                                                                                                                                                                                            
 99                     counter++;                                                                                                                                                                                                            
100                     numberRead++;                                                                                                                                                                                                         
101             }                                                                                                                                                                                                                             
102     }
4

3 回答 3

1
// this following code modification will handle your most recent question

int value = 0; // place to accumulate numbers
int inNumber = 0; // to avoid spurious input in array[]

// note: the following expects that 'ch' is defined as an integer
while ((ch =fgetc(fp)) != EOF)
{
    if (ch >= '0' && ch <= '9') // only process numeric characters
    {
        value *= 10;
        value += (ch - 0x30); // convert alpha to binary 
        inNumber = 1;
    }
    else
    { // break between numbers
        if( 1 == inNumber )
        {
            array[counter] = value;
            counter++;
            numberRead++;
            value = 0; // reset for next number
            inNumber = 0;
        }
    }
}
于 2014-10-07T14:33:41.523 回答
1

你应该在这里使用括号while (ch = fgetc(fp) != EOF && isdigit(ch))while ((ch = fgetc(fp)) != EOF && isdigit(ch))否则你将存储ch的值fgetc(fp) != EOF是 1 或 0(TRUE 或 FALSE)

于 2014-10-07T01:34:50.817 回答
1

为了扩展 Matt McNabb 在评论中所说的内容,你不能return没有值(除非它在void函数内部)。您的readNumbers()函数被声明为返回int,因此所有返回路径都必须返回一个 int。如果存在文件错误,您可能希望返回 -1,因为 0 是(类型 :))要读取的有效字符数。

由于输入文件中的数字之间存在空格,因此您需要更改while循环中的逻辑。

while ((ch = fgetc(fp)) != EOF && isdigit(ch))

一旦读取非数字字符就会失败。

我还应该提到,您正在将读取的每个字符的数值存储到数组中,这可能不是您想要的。例如,在 ASCII 中,“0”字符的数值为 48,“1”的数值为 49,以此类推。

PS。确保调用的函数readNumbers()提供了一个足够大的数组来处理任何可能的结果......


尽量避免exit()在你的程序中使用 deep,如果可行,只在main(). 此外,与其直接杀死你的程序,exit()不如打印某种错误消息(通常是到 stderr),然后优雅地死掉。至于创建合适的错误消息,请查看<stdio.h>功能perror(),并查看<errno.h>.

您可以打印错误消息readNumbers()并返回-1,然后让调用函数(例如main())决定错误是否严重到程序应该死掉。或者让调用函数也处理错误消息的打印。

于 2014-10-07T01:48:07.830 回答