简体   繁体   English

读取TEXT文件C

[英]Reading TEXT Files C

The text file below is the standard I am dealing with. 下面的文本文件是我正在处理的标准。

Basically I want to record how many time steps are involved throughout processing the below text file. 基本上,我想记录整个以下文本文件的处理过程涉及多少时间。

The lines that are of significance are the first line, and lines start with 'if'. 重要的行是第一行,并且行以“ if”开头。 'if' lines are handled as follows: if i < 3 i=i+1 goto 8 I am assuming i is initialised at 0. 'if'行的处理方式如下:如果i <3 i = i + 1转到8,则假定i初始化为0。

This means that the control should jump to line 8 as long as i is less than 3 这意味着只要我小于3,控件就应该跳到第8行

10
1fi
if i < 3 i=i+1 goto 8
3sdkfj
4ksdkk
5kdkfk
6kdkjf
7dkjkfd
if k < 2 k=k+1 goto 2
9dkkf
10dku
if j < 2 j=j+1 goto 2

My question being, using fopen to open the text file and fgets to gather the lines... how would I use fgets to go back to a line already processed by fgets ie doing what the if statement suggests in the above text file and go back to line 2. Without opening the text file again and doing whatever... 我的问题是,使用fopen打开文本文件并使用fgets收集行...我将如何使用fgets返回fgets已处理的行,即执行上述文本文件中的if语句建议并返回到第2行。无需再次打开文本文件并执行任何操作...

My code so far works to gather the first line and number of lines in any given text file within an in.file that looks like follows: 到目前为止,我的代码可以收集in.file中任何给定文本文件中的第一行和行数,如下所示:

./JobA.txt
./JobB.txt
./JobC.txt
./JobD.txt

My code: 我的代码:

    #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[BUFSIZ];
        sscanf(buffer,"./%s/", buf);
        cur = fopen(buf, "r" );

        int steps = 0;


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


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



            steps++;
        } 
        current->parameters.scheduled = steps;

        jobs++;

        fclose(cur);

    } 

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

}   


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

There are several possibilities to go backwards in a file: 在文件中有几种向后移动的可能性:

  1. Store all the lines of the file in an array (this doesn't scale very well!!) 将文件的所有行存储在一个数组中(扩展性不佳!)
  2. Close the file and reopen it, skipping to the nth line (lots of unnecessary opening and closing of files) 关闭文件并重新打开,跳到第n行(许多不必要的文件打开和关闭)
  3. Using fseek to go to the correct line (record the contents of ftell at the start of each line and then use fseek to go back to them). 使用fseek转到正确的行(在每行的开头记录ftell的内容,然后使用fseek返回到它们)。 This scales a lot better. 这样可以更好地扩展。

Here's some info in fseek and also on ftell 这是fseekftell上的一些信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM