Thread: Help with qsort

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    11

    Help with qsort

    Hey everyone,

    I'm a program newbie, I'm writing a code for sorting numbers from a input file,

    basically, I ask user to type in the file name, open file, read it, pass to a array, then sort it


    qsort(array, array_size, sizeof(int), (*compare)(const void *, const void *));


    since the array_size may vary for different file, I can't just use
    int array_size= some #

    How would I write this? Thank you very much!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well presumably you had a count which you incremented when reading the file.

    So use that count for the array_size when you call qsort()
    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
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    hmm.. I don't know how to make count increment as I read the file.

    If the program read a list of number and store to a char array called "instr"

    Can I do : int count = sizeof(instr)/sizeof(char);

    Thanks for helping me. This is my first time learning programing.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post more code, so we can see what you're doing.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    This is the code I have so far, took me along time to learn from googling. so basically, I open the file, read it, pass to a array, then sort it.

    It's long, so thanks for reading it.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main()
    {
        /*Promot user to enter file name*/
        char infile_name[20];
        printf("File:");
        scanf("%s", infile_name);
        printf("\n");
        
        /* Open the file */
        FILE *infile = fopen(infile_name, "r");
        if (!infile) 
        {
                    /* If the operation was unsuccessful */
                    printf("unable to open %s\n", infile_name);
                    exit(1); 
    
        }
        
        /* Read the file */
        int in;
        char instr;
        do
        {
            fscanf(infile, "%d", &in); /* read from infile into int variable in */
            if (!feof(infile))  /* only process in if didn't reach end of file */
           {
                instr=in; /* pass In to a array*/
           }
        } while(!feof(infile));
        fclose(infile);
    
        /* Sorting */
        int compare (const void *s, const void *t)
        {
            return (*(const int*)s - *(const int*)t);
        }
        int count =sizeof(instr)/sizeof(char);
        qsort(instr, count, sizeof(int), (*compare)(const void *, const void *));

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you need something like this for reading ints from a file.
    Code:
    int array[10];
    int count = 0;
    while ( count < 10 &&                             // room in the array
            fscanf(infile, "%d", &array[count]) == 1  // reading successful
          ) {
      count++;
    }
    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.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Quote Originally Posted by Salem View Post
    Well you need something like this for reading ints from a file.
    Code:
    int array[10];
    int count = 0;
    while ( count < 10 &&                             // room in the array
            fscanf(infile, "%d", &array[count]) == 1  // reading successful
          ) {
      count++;
    }
    My assignment kinda asked me to use feof to check, basically fscanf just like scanf but takes a extra (first) parameter, maybe the input file only has 1 number, so that's why I have if (!feof(infile)) to check if it's at the end of file.

    I can just add

    Code:
     
    int count = 0;
     if (!feof(infile))  /*if didn't reach end of file*/
     {
        count++;
     }
    But I think I'm actually discovered a problem:
    I have
    Code:
        /* Read the file */
        int in;
        char instr;
        do
        {
            fscanf(infile, "%d", &in); /* read from infile into int variable in */
            if (!feof(infile))  /* only process  if didn't reach end of file */
           {
                instr=in; /* process in */
           }
        } while(!feof(infile));
        fclose(infile);
    I think with instr=in; It will just replace the first number passed from in, how do I make a loop so it won't replace?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to put the value you read, into an array. I don't see anywhere where you set up an array, so step 1 would be "declare an array".

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by muqing View Post
    My assignment kinda asked me to use feof to check,
    so use it just to check!!!

    Code:
    int array[10];
    int count = 0;
    while ( count < 10 &&                             // room in the array
            fscanf(infile, "%d", &array[count]) == 1  // reading successful
          ) {
      count++;
    }
    
    
    if(feof(infile))
    {
       puts("End of file reached");
    }
    else
    {
       puts("Something could be left in the file");
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Quote Originally Posted by tabstop View Post
    You need to put the value you read, into an array. I don't see anywhere where you set up an array, so step 1 would be "declare an array".
    so If I set a char array, what shoud i use to pass the vaule stored to "in" to that char array?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You shouldn't pass the values into a char array, since you are not reading chars.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    sorry, my mistake there.

    so If I set up a int array,

    and I use fscanf(infile, "%d", &in); to store into interger variable "in".

    If The file didn't reach to the end if (!feof(infile))

    how should I process the variable "in"?

    I assme I just need to pass "in" to a array, how should I do that?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By assigning the newly-read value to the next open spot in the array (which is what your counter will tell you).

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Thanks for helping me tabstop
    since the array size if determined by unknown input file, can I do something like this:

    Code:
    int i, in;
    
    int[] array;
        do
        {
            fscanf(infile, "%d", &in); /* read from infile into int variable in */
            if (!feof(infile))  /* only process in if didn't reach end of file */
           {
                for (i=1; i<=sizeof(in), i++)
                int array[i]=in; /* process in */
           }
        } while(!feof(infile));
        fclose(infile);

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nope. If you don't know a limit ahead of time, then you'll have to use a "dynamic array", i.e., a big chunk of memory that you get from malloc or calloc or realloc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  3. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 02:22 AM
  4. qsort - newbie trouble with pointers
    By aze in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 03:38 PM
  5. Qsort
    By Tombear in forum C Programming
    Replies: 1
    Last Post: 10-28-2001, 09:06 AM