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 6803795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:23:17+00:00 2026-05-26T19:23:17+00:00

I have an exercise where I am required to print a file slowly (1

  • 0

I have an exercise where I am required to print a file slowly (1 second intervals) until the file ends, unless the user types a character.

So far, the program outputs the file in one second intervals which is great, but when I type a character, nothing happens. My guess is that I am using select wrong somehow.

This is the final program I ended up submitting.

#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    FILE* infile;
    char str[100];
    fd_set readset;
    struct timeval tv;

    // open a file
    if((infile = fopen("infile", "r")) == NULL)
    {
        (void)printf("Couldn't open the file\n");
        exit(1);
    }
    // file was opened successfully
    else
    {       
        // while we are not at the end of a file
        while(fgets(str, 100, infile) != NULL)
        {
            FD_ZERO(&readset);
            FD_SET(fileno(stdin), &readset);
            // set the time value to 1 second
            tv.tv_sec = 1;
            tv.tv_usec = 0;
            select(fileno(infile)+1, &readset, NULL, NULL, &tv);
            // the user typed a character so exit
            if(FD_ISSET(fileno(stdin), &readset))
            {
                fclose(infile);
                exit(0);
            }
            // the user didn't type a character so print the next line
            else
            {
                fgets(str, 100, stdin);
                puts(str);
            }
        }

        // clean up
        fclose(infile);
    }

    // report success
    return 0;
}

Thanks for the help!

  • 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-05-26T19:23:17+00:00Added an answer on May 26, 2026 at 7:23 pm

    This is a working version, using tcgetattr/tcsetattr:

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/select.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <termios.h>
    
    int main(void) {
        FILE* infile;
        char str[100];
        fd_set readset;
        struct timeval tv;
        struct termios ttystate, ttysave;
    
        // open a file
        if((infile = fopen("infile", "r")) == NULL)
        {
            (void)printf("Couldn't open the file\n");
            exit(1);
        }
        // file was opened successfully
    
        //get the terminal state
        tcgetattr(STDIN_FILENO, &ttystate);
        ttysave = ttystate;
        //turn off canonical mode and echo
        ttystate.c_lflag &= ~(ICANON | ECHO);
        //minimum of number input read.
        ttystate.c_cc[VMIN] = 1;
    
        //set the terminal attributes.
        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
    
        // while we are not at the end of a file
        while(fgets (str, 100, infile))
        {
            // set the time value to 1 second
            tv.tv_sec = 1;
            tv.tv_usec = 0;
    
            FD_ZERO(&readset);
            FD_SET(fileno(stdin), &readset);
    
            select(fileno(stdin)+1, &readset, NULL, NULL, &tv);
            // the user typed a character so exit
            if(FD_ISSET(fileno(stdin), &readset))
            {
                fgetc (stdin); // discard character
                break;
            }
            // the user didn't type a character so print the next line
            else
            {
                puts(str);
                // not needed: sleep(1);
            }
        }
    
        // clean up
        fclose(infile);
    
        ttystate.c_lflag |= ICANON | ECHO;
        //set the terminal attributes.
        tcsetattr(STDIN_FILENO, TCSANOW, &ttysave);
        // report success
        return 0;
    }
    

    The sleep(1); isn’t needed anymore.

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

Sidebar

Related Questions

I have the exercise with following code int FindFirstSet(unsigned BitMap, unsigned start) { unsigned
I have a simple exercise where I call a Thread and subsequently the run()
During my university exercise I have come across strange behaviour of a variable. /*
I have been given an exercise to solve the zebra puzzle using a constraint
I have been busy with a exercise where I need to compare a winning
i have to crawl last.fm for users (university exercise). I'm new to python and
Let's say I have a large query (for the purposes of this exercise say
I have a code like this, which is solved by me following a exercise
I am writing a huffman implementation in Python as a learning exercise. I have
I am using the exercise package to display exercises in a book. I have

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.