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.

3
  • Please, try reading the file in binary mode and not text mode or use fgets(...). Commented Oct 7, 2014 at 3: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. Commented Oct 7, 2014 at 4: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 :-) Commented Oct 7, 2014 at 5:27

2 Answers 2

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 , " ");
}
Sign up to request clarification or add additional context in comments.

2 Comments

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

1 Comment

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?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.