qsort for structures c

时间:2012-10-09 12:56:09

标签: c text qsort

以下是我的代码:

我似乎无法有效地使用qsort ...它在填充了名称和开始时间之后将我的数组变为0 ...这是我的qsort调用的问题吗?或者qsort本身。

具有结构的标题如下:

 /**
 * Simulation of a process scheduler
*/

//#ifndef SCHEDULER_H_
#define SCHEDULER_H_

#include <stddef.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>


/* types */
/** units of time */
typedef long time;
/** process identifier */
typedef int pid;

/** Information about a job of interest to the task scheduler */
struct job_data {

/* pid of this process */
    pid pid;
    /* time process starts */
    time start;
    /* time needed to finish */
    time finish;
    /* time spent processing so far */
    time scheduled;
    /* Number of lines */
    int lines;

};

struct job {

    /* Various parameters used by the scheduler */
    char job_id[20];
    struct job_data parameters;
    char *lines[20];


};  




      /* I/O Files */
        //static char *inputFile;
        char * in;
        static FILE *input;
        static FILE *cur;
        /*Scheduled jobs indexed by PID*/
        struct job list[20];

        /* the next job to schedule */
        //static struct job *job_next = NULL;

        /* Time */
        time clock;

        /*Comparison for qsort*/
        int compare_start(const void *x, const void *y)
        {
            const struct job *a = x;
            const struct job *b = y;

                printf("%ld, %ld\n", a->parameters.start, b->parameters.start);

             if (a->parameters.start < b->parameters.start) 
            {
                        return -1;
             }
             if (a->parameters.start > b->parameters.start) 
            {
                    return 1;
             }

            return 0;

        }

        /*Order Jobs*/
        static void order_jobs(void)
        {

            qsort(list, (sizeof list) / (sizeof list[0]), sizeof list[0], compare_start);   

        }


        /** Read and parse input from input file */
        static void parse_input(void) 
        {
            char    buffer[BUFSIZ];
            char    lines[BUFSIZ];
            int jobs = 0;
            struct  job *current;





            while( fgets(buffer, sizeof(buffer), input) )   
            {


                time start;
                char buf[BUFSIZ];
                sscanf(buffer,"./%s/", buf);
                cur = fopen(buf, "r" );

                int n_lines = 0;


                while( fgets(lines, sizeof(lines), cur) )
                {


                    if( n_lines == 0 )
                    {
                        current = &list[jobs];
                        strcpy(current->job_id, buf);
                        sscanf(lines,"%ld", &start);
                        current->parameters.start = start;              
                    }       

                    n_lines++;
                } 
                current->parameters.lines = n_lines;

                jobs++;

                fclose(cur);

            } 
            order_jobs();
            for (int i = 0; i < jobs; i++)
            {
                printf("%s %ld  %d\n", list[i].job_id, list[i].parameters.start, list[i].parameters.lines);
            }   

        }

int main(int argc, char **argv) 
{
    in = argv[1];
    if ( (input = fopen(in, "r")) == NULL ) {
        fprintf(stderr, "cannot open %s\n", argv[1]);
    }

    parse_input();

    fclose(input);

    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:5)

您只需将jobs个条目加载到数组中,但是您告诉qsort()对整个数组进行排序(20个元素)。这可能会将未初始化的元素放在前面,然后打印出来。