Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:40:19+00:00 2026-06-12T17:40:19+00:00

The text file below is the standard I am dealing with. Basically I want

  • 0

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 i.e. 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:40:21+00:00Added an answer on June 12, 2026 at 5:40 pm

    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)
    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). This scales a lot better.

    Here’s some info in fseek and also on ftell

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically the code below reads in a text file and diplays it on the
I've got around 10 lines of data in a text file below containing the
okay, so i have a text file with example content below line1with some random
Below is an excerpt from an .svg file (which is xml): <text xml:space=preserve style=font-size:14.19380379px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;font-family:DejaVu
I have a file (called print_1012720.txt ) that looks like the text shown below.
My text file contains 2 lines: <IMG SRC=/icons/folder.gif ALT=[DIR]> <A HREF=yahoo.com.jp/>yahoo.com.jp/</A> </PRE><HR> In my
I have a text file that I want to edit by rewriting it to
The text below is an output in text format. I want to retain or
i have a text file like below : string string string string string string
I'm trying to read specific values from the text file (below): Current Online Users:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.