Processing Input file in c

2019-09-22 06:53发布

问题:

So Im just wondering about a few tips on doing a scheduler simulation.

So far, I just want to input a file at the command line i.e. /.scheduler in.file

The in.file holds the following information:

./Job1.txt
./Job2.txt
./Job3.txt
./Job4.txt

Each Job .txt file has random lines of code. With only the first line being significant. The first line is starting 'tick' time.

Job A:

10
1fi
3sdkfj
4ksdkk
5kdkfk
6kdkjf
7dkjkfd
9dkkf
10dku

At the moment I just want to take the in.file and order the 'Job' files in order of their arrival tick time i.e. first line.

My code thus far:

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


#include "projscheduler.h"



/* I/O Files */
//static char *inputFile;
char * in;
static FILE *input;

/*Scheduled jobs indexed by PID*/
struct job list[20];

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

/* Time */
time clock;

/*Initialises job list*/
static void initialise_list(void) {
    for(int i = 0; i < sizeof(list)/sizeof(list[0]); i++) {
        list[i].parameters.pid = -1;
    }
}

/** Read and parse input from input file */
static void parse_input(void) 
{
    char    buffer[BUFSIZ];
    //int   jobs;



    initialise_list();



    while( fgets(buffer, sizeof(buffer), input) )   
    {
        pid j_pid;
        sscanf(buffer, "./%d.txt", &j_pid);

    } 

}   


int main(int argc, char **argv) 
{

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

    parse_input(); 


    return EXIT_SUCCESS;
}

HEADER FILE:

/**
 * Simulation of a process scheduler
*/

//#ifndef SCHEDULER_H_
#define SCHEDULER_H_

#include <stddef.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;
    /* size of the process */
    size_t size;

};

struct job {

    /* Various parameters used by the scheduler */
    char job_name[BUFSIZ];
    struct job_data parameters;
    /* next job to be scheduled */
    //struct job *next;


};

回答1:

I am unsure what problems you are having exactly but I can see the following errors in the code:

  • In the initialise_list() function the for loop will be iterating too many times and going beyond the bounds the of array:

    for(int i = 0; i < sizeof(list); i++) {
    

as sizeof(list) will return the number of bytes occupied by the array which is 20 * sizeof(struct job). Change to:

    for(int i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
  • this is a missing left parenthesis from fopen() attempt (meaning the posted code does not compile):

    if( (input = fopen(inputfile, "r") == NULL )
    

should be:

    if( (input = fopen(inputfile, "r")) == NULL )
  • proj is an initialised char* and this will mostly likely cause a segmentation fault:

    char *proj;
    sscanf(buffer, "./%s.txt", proj);
    


回答2:

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


#include "projscheduler.h"



/* 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;

/*Initialises job list*/
static void initialise_list(void) {
    for(int i = 0; i < sizeof(list)/sizeof(list[0]); i++) {
        list[i].parameters.pid = -1;
    }
}

/*Order Jobs*/
/*=static void order_jobs(void)
{
    for(int i=0; i < sizeof(list)/sizeof(list[0]); i++)
    {


}
*/

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


    initialise_list();



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


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





        fgets(lines, sizeof(lines), cur);
        sscanf(lines,"%ld", &start);


        current = &list[jobs];

        current->job_id = buf;
        current->parameters.start = start;

        jobs++;

    } 

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

}   


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();



    return EXIT_SUCCESS;
}

Above, I successfully read the in.file and extracted the first line from each address representing a text file.

Code above associated with following header:

/**
 * Simulation of a process scheduler
*/

//#ifndef SCHEDULER_H_
#define SCHEDULER_H_

#include <stddef.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;
    /* size of the process */
    size_t size;

};

struct job {

    /* Various parameters used by the scheduler */
    char * job_id;
    struct job_data parameters;
    /* next job to be scheduled */
    //struct job *next;


};