funny output at the end of textfile after sort.

This is a discussion on funny output at the end of textfile after sort. within the C Programming forums, part of the General Programming Boards category; Hi everyone. I am almost finished with this assignment but I am getting some extra numbers at the very end ...

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

    funny output at the end of textfile after sort.

    Hi everyone. I am almost finished with this assignment but I am getting some extra numbers at the very end of my output file after the integers are sorted. the output file looks like:
    Code:
    0
    1
    2
    3
    .
    .
    .
    98
    99
    1297040174
    My main looks like this:
    Code:
    #include "header.h"
    
    int main(int argc, const char *argv[]) {
        int i = 0, N = 0, *array, k, dummy;
        //hrtime_t start, end;
    
        FILE *input;
        FILE *output;
    
        input = fopen("100_random.txt", "r");
        if (input == NULL) {
            fprintf(stderr, "Cannot open file for reading\n");
            exit(EXIT_FAILURE);
        }
    
        while (!feof(input)) {
            fscanf(input, "%d", &dummy);
            N++;
    }
        if (fclose(input)) {
            fprintf(stderr, "Error closing the file.\n");
            exit(EXIT_FAILURE);
        }
    
        array = (int*) malloc(N* sizeof(int));
        if(array == NULL)
            printf("Error - could not allocate an array.\n");
        else {
            input = fopen("100_random.txt", "r");
            for(k = 0; k < N; k++)
                fscanf(input, "%d", &(array[k]));
            fclose(input);
        }
    
         //start = gethrtime();
         //insertion_sort(array,i);
         //end = gethrtime();
         //printf("The average time for the insertion_sort function was: %11d nsec.\n", (end - start)/n);
         //selection_sort(array, i);
         bubble_sort(array, k);
         //mod_bubble_sort(array, i);
         //quick_sort(array, 0, i - 1);
         //merge_sort(array, 0, i -1);
    
        output = fopen("out.txt", "w+");
        for (i = 0; i < N; i++){
    
        fprintf(output, "%d\n", array[i]);
    }
    
        free (array);
        array = NULL;
    
        return (EXIT_SUCCESS);
    }
    I have to sort 3 different sized arrays:
    1) 100 ints
    2) 1000 ints
    3) 10000 ints

    I noticed I do NOT get the extra numbers at the end of the 10000 int sort. Just the 100 and 1000.
    I have to say, that the output is not do to the sorting functions. It happened when I just wrote to the file NOT using the sort funtions.

    Anyone have any idea how to solve this and what might be wrong?

    The sort functions should be correct as I have tested them. But if you need to see them, let me know.

    UPDATE: I ran this on our unix system and I do NOT get the extra line with numbers on it. I'm thinking it might be CodeBlocks. If I am incorrect, I am still willing to listen to reasons and solutions.
    Last edited by csharp100; 10-20-2012 at 04:50 PM. Reason: additional information

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,781
    That is probably being caused by this line:
    Code:
     while (!feof(input))
    Using feof() to control a loop will usually cause this sort of problem. You may want to check the FAQ for more information relating to this issue. Try printing the value of your counter after you leave the loop, what does it contain?

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yep ^^^.

    fscanf() will return the number of items it has stored. So use that, to control your loop, and forget about feof.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Adak View Post
    Yep ^^^.

    fscanf() will return the number of items it has stored. So use that, to control your loop, and forget about feof.
    Adak, can you show me an example as it pertains to what I have please? I would appreciate it.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by csharp100 View Post
    Adak, can you show me an example as it pertains to what I have please? I would appreciate it.
    Make the file:
    Open a command window: <shift key> + <window key> + <right mouse button> select "open command window here"

    then type: copy con data.txt <enter>
    1<enter>
    2<enter>
    3<enter>
    4<enter>
    5<enter>
    <ctrl+z> <enter>

    Then compile and run this


    Code:
    #include <stdio.h>
    
    int main(void) {
       int i;
       FILE *fp=fopen("data.txt","r");
       if(fp) {
          while(fscanf(fp, "%d",&i)> 0) { 
             printf("%d\n",i);
          }
           fclose(fp);
       }
       printf("\n");
       return 0;
    }
    The above while loop replaces your programs file loop, beginning on your line #16.
    Last edited by Adak; 10-21-2012 at 05:04 PM.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Adak View Post
    Make the file:
    Open a command window: <shift key> + <window key> + <right mouse button> select "open command window here"

    then type: copy con data.txt <enter>
    1<enter>
    2<enter>
    3<enter>
    4<enter>
    5<enter>
    <ctrl+z> <enter>

    Then compile and run this


    Code:
    #include <stdio.h>
    
    int main(void) {
       int i;
       FILE *fp=fopen("data.txt","r");
       if(fp) {
          while(fscanf(fp, "%d",&i)> 0) { 
             printf("%d\n",i);
          }
       }
       fclose(fp);   
       printf("\n");
       return 0;
    }
    The above while loop replaces your programs file loop, beginning on your line #16.
    Hehe, Thanks Adak. I appreciate it. Do you now much about gethrtime? I have made a new post in regards to not getting any result from it.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome. I don't see gethrtime listed for Pelles C. Where did you run into it? Your post is not showing in the C forum. What forum did you post it in?
    Last edited by Adak; 10-21-2012 at 05:12 PM.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Adak View Post
    You're welcome. I don't see gethrtime listed for Pelles C. Where did you run into it?
    Solaris. That is what our university's campus computer runs. I think I might have it fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-15-2012, 03:06 PM
  2. Help With Funny Integer Output
    By Spamtacus in forum C Programming
    Replies: 16
    Last Post: 11-26-2011, 12:22 PM
  3. array sort output problem
    By radiantarchon28 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2007, 01:49 AM
  4. Merge Sort - Invalid Output ??
    By gqchynaboy in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2005, 05:01 PM
  5. Funny output on hex value of number
    By shav in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 07:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21