Thread: Sorting numbers in a file

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>quicksort(array, s);
    You don't want to sort the whole array, only the elements that have real data in them, so use:
    >>quicksort(array, i);

    Change this bit:
    Code:
    while(array != NULL){      //Puts the numbers back into a new file
          for(n=0; n <101; n++){
              fprintf(quick, "%d\n", array[n]);
          }
        }
    to this (note, no "while" loop needed)
    Code:
    for (n = 0; n < i; n++)
      {
        fprintf(quick, "%d\n", array[n]);
      }
    Change this
    >>for(b=a=1;b<size;b++){
    to this
    for (b = a; b < size; b++)

    See how you go, that may not be all necessary changes, but it should get you moving.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Wow, I didn't even see those things. Thanks for the start.

    Actually, those things fixed the whole program. Thanks.

  3. #18
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    For loop is not appropriate in this case.
    Code:
    for (i=0; fgets(buf,sizeof(buf),fp) && i < 101; i++){
      array[i]=atoi(buf);
    }
    Use this:
    Code:
    i = 0;
    while (fgets(buf,sizeof(buf),fp) != EOF)
    {
        array[i++]=atoi(buf);
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #19
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by WaltP
    For loop is not appropriate in this case.
    Code:
    for (i=0; fgets(buf,sizeof(buf),fp) && i < 101; i++){
      array[i]=atoi(buf);
    }
    Use this:
    Code:
    i = 0;
    while (fgets(buf,sizeof(buf),fp) != EOF)
    {
        array[i++]=atoi(buf);
    }
    this could cause an overflow as you didnt add the i < 101...also i gets incremented past the end of the array on the last recursion (if there is 100 numbers) so if you referance array[i] again after the loop without changing i, it would cause undefined behavior.

    Why isnt a for loop apporiate? do you just have a general distaste for for loops, or what?

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while (fgets(buf,sizeof(buf),fp) != EOF)
    You probably meant NULL instead of EOF.

    >For loop is not appropriate in this case.
    Why not? It still counts a set number of times unless end of file is reached or an error occurs. If it makes you feel better then you can move fgets out of the loop condition to get something more idiomatic (albeit also more verbose):
    Code:
    for (i=0; i < 101; i++){
      if (fgets(buf,sizeof(buf),fp) == NULL)
        break;
      array[i]=atoi(buf);
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM