【问题标题】:Processing Input file in c在c中处理输入文件
【发布时间】:2012-09-28 17:12:53
【问题描述】:

所以我只是想知道一些关于进行调度程序模拟的技巧。

到目前为止,我只想在命令行输入一个文件,即 /.scheduler in.file

in.file 包含以下信息:

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

每个 Job .txt 文件都有随机的代码行。只有第一行很重要。第一行是开始“滴答”时间。

工作 A:

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

目前我只想获取 in.file 并按照到达时间的顺序排列“作业”文件,即第一行。

到目前为止我的代码:

#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;
}

头文件:

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


};

【问题讨论】:

  • 也许这应该继续进行代码审查。
  • 我的错!对不起,我只是在睡觉时回顾它!我取得了进展...

标签: c file parsing input


【解决方案1】:

我不确定您到底遇到了什么问题,但我可以在代码中看到以下错误:

  • initialise_list() 函数中,for 循环将迭代太多次并超出数组的范围:

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

因为sizeof(list) 将返回数组占用的字节数20 * sizeof(struct job)。改为:

    for(int i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
  • 这是 fopen() 尝试中缺少的左括号(意味着发布的代码无法编译):

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

应该是:

    if( (input = fopen(inputfile, "r")) == NULL )
  • proj 是初始化的char*,这很可能会导致分段错误:

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

【讨论】:

  • 我的问题是我不确定如何打开文件“file.in”并从与可执行文件位于同一文件夹中的作业文本文件中收集信息。
  • 谢谢大家,我现在明白了第一个 for 循环会产生问题......现在就剩下的 :(
  • 我正在通过您的帮助更新我的代码...让我们知道您的想法!欢呼传奇
  • 有一件事我不清楚... in.file 基本上有文本文件,由那里的地址表示。我想接受工作 pid,即 JobA 然后还从该文本文件中获取“勾号”......我不知道该怎么做。
【解决方案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;
}

上面,我成功读取了 in.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_id;
    struct job_data parameters;
    /* next job to be scheduled */
    //struct job *next;


};

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多