2014-10-07 125 views
0

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

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等字符解释为55。所以我的阵列将有两个5's。

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

将'ch = fgetc(fp)!= EOF'改为'(ch = fgetc(fp))!= EOF'。比较运算符的优先级高于赋值。 – 2014-10-07 01:34:23

+0

您的函数在没有关闭文件的情况下返回,以防'有点奇怪的'ferror'。 “无回应”也是非法的,没有争论。 – 2014-10-07 01:35:21

+0

@MattMcNabb谢谢! – mrQWERTY 2014-10-07 01:37:14

回答

1

要什么马特·麦克纳布扩大在评论中说,你不能有没有值的return(除非它在void函数中)。您的readNumbers()函数声明为返回int所以全部返回路径必须返回一个int。如果存在文件错误,您可能希望返回-1,因为0是(种类:))要读取的有效字符数。

由于输入文件中数字之间有空格,所以您需要更改while循环中的逻辑。在读取非数字字符

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

就会失败。

我还应该提到,你正在存储你读到数组中的每个字符的数值,这可能不是你想要的。例如,在ASCII中,'0'字符将具有48的数值,'1'具有值49,等等。

PS。请确保调用readNumbers()功能提供了足够大,以处理任何可能的结果数组...


尽量避免使用你的程序中exit()深,实用的时候,只是用它main()。此外,而不是只是用exit()杀死你的程序石头死亡,它的更好打印某种错误消息(通常为stderr),然后然后优雅地死去。至于创建合适的错误消息,请查看<stdio.h>函数perror(),并查看<errno.h>

您可以在readNumbers()中打印错误消息并返回-1,然后让调用函数(例如main())确定错误是否如此糟糕以至于程序应该死亡。或者让调用函数也处理错误消息的打印。

+0

太棒了,如何使用退出(0)代替返回-1? – mrQWERTY 2014-10-07 02:03:02

1

你应该我们这里的括号while (ch = fgetc(fp) != EOF && isdigit(ch)),它应该是while ((ch = fgetc(fp)) != EOF && isdigit(ch))否则你会在ch存储fgetc(fp) != EOF的值,它是eather 1或0(TRUE或FALSE)

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; 
     } 
    } 
}