2

I am having trouble converting a string of numbers from a file, say 1 2 55 -44 22 into an integer array.

My program so far works except for one bug, it interprets integers like 55 as 5 and 5.

    int readNumbers(int array[], char* fname) {                                                                                                                                                                                               
 78     // TODO: will you read the comments above this function carefully???                                                                                                                                                                  
 79     // TODO: note those pre- and post-conditions!                                                                                                                                                                                         
 80     int numberRead = 0;                                                                                                                                                                                                                   
 81     FILE* fp;                                                                                                                                                                                                                             
 82     int ch;                                                                                                                                                                                                                               
 83     int counter = 0;                                                                                                                                                                                                                      
 84                                                                                                                                                                                                                                           
 85     // Use fopen to try to open the file for reading                                                                                                                                                                                      
 86     // TODO:                                                                                                                                                                                                                              
 87     fp = fopen(fname, "r");                                                                                                                                                                                                               
 88     // Test to see if the file was opened correctly                                                                                                                                                                                       
 89     // TODO:                                                                                                                                                                                                                              
 90     if (fp == NULL) {                                                                                                                                                                                                                     
 91             printf("Error opening file\n");                                                                                                                                                                                               
 92             return -1;                                                                                                                                                                                                                    
 93     }                                                                                                                                                                                                                                     
 94     // Now read until end of file                                                                                                                                                                                                         
 95     // TODO:                                                                                                                                                                                                                              
 96     while ((ch = fgetc(fp)) != EOF) {                                                                                                                                                                                                     
 97             if (ch != ' ' && ch != '\n') {                                                                                                                                                                                                
 98                     array[counter] = ch - '0';                                                                                                                                                                                            
 99                     counter++;                                                                                                                                                                                                            
100                     numberRead++;                                                                                                                                                                                                         
101             }                                                                                                                                                                                                                             
102     }                                                                                                                                                                                                                                     
103     if (ferror(fp)) {                                                                                                                                                                                                                     
104             fclose(fp);                                                                                                                                                                                                                   
105             return -1;                                                                                                                                                                                                                    
106     }                                                                                                                                                                                                                                     
107     // Close the file pointer                                                                                                                                                                                                             
108     // TODO:                                                                                                                                                                                                                              
109     fclose(fp);                                                                                                                                                                                                                           
110                                                                                                                                                                                                                                           
111     // Return the number of items read                                                                                                                                                                                                    
112     return numberRead;  // can it be negative? if in doubt, read.                                                                                                                                                                         
113 } 

I've looked elsewhere and many use fgets? I'm not sure if that would make a difference and want to hear opinions before making a change.

mrQWERTY
  • 4,039
  • 13
  • 43
  • 91
  • Please, try reading the file in `binary mode` and not `text mode` or use `fgets(...)`. – nIcE cOw Oct 07 '14 at 03:20
  • 1
    @nIcEcOw I see where you're coming from, but from an earlier question by Renren29 it appears that the numeric data is in plain ASCII in a text file. – PM 2Ring Oct 07 '14 at 04:56
  • @PM2Ring: Haha, I thought about that once, after writing my comment, as to what if - __"The file is a text file/Already written in text mode"__. That is why I kept it as comment. But Thank You, for bringing this to the front of my knowledge bank. Hopefully now I won't forget this, while giving some idea to someone :-) – nIcE cOw Oct 07 '14 at 05:27

2 Answers2

3

here's how you can do it with fgets

char arr[PickYourSize];
char* ptr;
fgets(arr , sizeof arr , fp);
ptr = strtok(arr , " ");
while(ptr)
{
       array[counter++] = strtol(ptr , NULL , 10);
       ++numberRead;
       ptr = strtok(NULL , " ");
}
Farouq Jouti
  • 1,657
  • 9
  • 15
  • Hi, I have one question. I've noticed that the size of arr leads to the number characters that are tokenized and accepted into the array. How do I make the size dependent on the number of characters in the file? For instance, if I put 20 as PickYourSize, then only part of the characters in the file are read. – mrQWERTY Oct 07 '14 at 03:45
  • well you can do a dynamic allocation , but before that you need to write a function to count the number of numbers in the file to allocate accordingly – Farouq Jouti Oct 07 '14 at 03:50
2

Your are using fgetc. It will do character reading. So you are facing cumbersome to meet your requirements. I request you to use fscanf and this is one of easy way too.

fscanf will return EOF if it fails before matching any of the arguments

Example code

int main ()
{
    FILE *fp = fopen ("/home/inputs.in", "r");
    int d=0;

    while ( EOF != fscanf ( fp, "%d ", &d ))
    {
            printf ("%d ", d);
    }
    return 0;
}
mahendiran.b
  • 1,315
  • 7
  • 13
  • Due to the way they handle whitespace, including `'\n'`, `fscanf()` and `scanf()` are horrible, both for the programmer and the user. But once the data has been read (eg by using 'fgets()') `sscanf()` can be useful. See [Reading a line using scanf() not good?](http://stackoverflow.com/questions/17294809/reading-a-line-using-scanf-not-good). – PM 2Ring Oct 07 '14 at 05:08