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' lines are handled as follows: if i < 3 i=i+1 goto 8 I am assuming i is initialised at 0.
This means that the control should jump to line 8 as long as i is less than 3
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...
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:
./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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.