Thread: problem with reading ints from a text file into an array.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    problem with reading ints from a text file into an array.

    Hello everyone. I am having a problem with reading a textfile of ints into an array for sorting. The text file has the format of:
    Code:
    1
    2
    3
    .
    .
    .
    My main function is:
    Code:
    #include "header.h"
    
    int main(void) {
        int i, array[SIZE];
        char buf[SIZE];
    
        FILE *fp;
    
        fp = fopen("100_sorted.txt", "r");
        if (fp ==NULL) {
            printf("Cannot open file for reading");
            exit(EXIT_FAILURE);
        }
        fgets(buf, sizeof(buf), fp);
        for (i = 0; i < SIZE; i++){
            sscanf(buf, "%d", &array[i]);
        }
        fclose(fp);
    
         insertion_sort(array,i);
         //selection_sort(array, n);
         //bubble_sort(array, n);
         //mod_bubble_sort(array, n);
         //quick_sort(array, 0, n - 1);
         //merge_sort(array, 0, n -1);
    
        for (i = 0; i < SIZE; i++){
            printf("%d\n", array[i]);
        }
        return (EXIT_SUCCESS);
    }
    The sort functions work correctly as I have tried them with a simple loop and array.

    What is happening is it is seems to read the first int which is "1" and loops the "1" continously instead of reasing the next line.

    I know it is probably something rather simple but I have been at it for a few hours and need a fresh set of eyes to take a looksee.

    I would appreciate if someone could tell me what I need to do to fix it.
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you've got 1 integer per line, it's
    Code:
    while ( i < SIZE && fgets(buf, sizeof(buf), fp) != NULL ) {
        if ( sscanf(buf, "%d", &array[i]) == 1 ) {
            i++;
        } else {
            fprintf(stderr, "garbage %s", buf );
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Let me go over your code, and show you exactly what is wrong.
    Quote Originally Posted by csharp100 View Post
    Code:
            printf("Cannot open file for reading");
    You forgot the newline at end of the line. Also, it would be better to write to standard error. That way, if the user redirected the expected output to a file, they will still see the error messages. So:
    Code:
            fprintf(stderr, "Cannot open file for reading.\n");
    That was not really an issue, but it is so useful in practice I thought I'd point it out anyway.

    The real problems are in the following:
    Quote Originally Posted by csharp100 View Post
    Code:
        fgets(buf, sizeof(buf), fp);
        for (i = 0; i < SIZE; i++){
            sscanf(buf, "%d", &array[i]);
        }
    First, you are reading just one line, then parsing it again and again in the loop.

    Second, you don't check the fgets() return value; it may return NULL if it cannot read the line.

    Third, you don't check the sscanf() return value to see if it did the conversion or not.

    I'd recommend you add a new variable, say char *line;, and moved the fgets() inside the loop:
    Code:
            line = fgets(buf, sizeof buf, fp);
            if (!line) {
                fprintf(stderr, "Cannot read line %d.\n", i + 1);
                exit(EXIT_FAILURE);
            }
    Similarly, to check that sscanf() really did do a conversion, check its return value -- the number of conversions you require from it:
    Code:
            if (sscanf(line, "%d", &array[i]) < 1) {
                fprintf(stderr, "Line %d does not start with an integer.\n", i + 1);
                exit(EXIT_FAILURE);
            }
    If you want to be thorough, you could also check the fclose() return value. You really should check it whenever you write to a file. It will return zero if there were no problems, and nonzero (EOF) if there is a problem. I'd recommend
    Code:
        if (fclose(fp)) {
            fprintf(stderr, "Error closing the file.\n");
            exit(EXIT_FAILURE);
        }

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Thank both of you. All was taken into consideration. It is functioning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from text file to array help please!
    By hotshotennis in forum C Programming
    Replies: 9
    Last Post: 05-25-2012, 03:14 PM
  2. Reading ints from text file to an array
    By hotshotennis in forum C Programming
    Replies: 4
    Last Post: 04-23-2012, 12:33 PM
  3. Reading in text file to an array?
    By Le23ron in forum C Programming
    Replies: 15
    Last Post: 02-12-2012, 11:02 PM
  4. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 06:45 AM
  5. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM