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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:08:58+00:00 2026-06-04T04:08:58+00:00

#include <stdio.h> #include <unistd.h> #include <sys/time.h> #include <sys/types.h> int main () { char name[20];

  • 0
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>

int main ()
{
char            name[20];
fd_set          input_set;
struct timeval  timeout;
int             ready_for_reading = 0;
int             read_bytes = 0;

/* Empty the FD Set */
FD_ZERO(&input_set );
/* Listen to the input descriptor */
FD_SET(0, &input_set);

/* Waiting for some seconds */
timeout.tv_sec = 10;    // 10 seconds
timeout.tv_usec = 0;    // 0 milliseconds

/* Invitation for the user to write something */
printf("Enter Username: (in 15 seconds)\n");
printf("Time start now!!!\n");

/* Listening for input stream for any activity */
ready_for_reading = select(1, &input_set, NULL, NULL, &timeout);
/* Here, first parameter is value of the socket descriptor + 1 (STDIN descriptor is 0, so  
 * 0 +1 = 1)  
 * in the set, second is our FD set for reading,
 * third is the FD set in which any write activity needs to updated, which is not required
 * in this case. Fourth is timeout
 */

if (ready_for_reading == -1) {
    /* Some error has occured in input */
    printf("Unable to read your input\n");
    return -1;
} else {
    if (ready_for_reading) {
        read_bytes = read(0, name, 19);
        printf("Read, %d bytes from input : %s \n", read_bytes, name);
    } else {
        printf(" 10 Seconds are over - no data input \n");
    }
}

return 0;

}

How to do the same, but not just once, but in infinite loop which breaks after encountering ‘quit’ string (for example). Every way I tried – failed.
So if no data has been inputed after 10 seconds program just prints “10 secs are over – no data input” and then starts waiting again. Same after input – just begins again and behave the same every time in infinite loop.
Am little desperate already, please – help.
Thanks.

  • 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-04T04:08:59+00:00Added an answer on June 4, 2026 at 4:08 am

    I don’t really see the problem here. Basically just put everything you want in the loop, and let it run. Did you try this?

    int main ()
    {
       /* Declarations and stuff */
       /* ... */
    
       /* The loop */
       int break_condition = 0;
       while (!break_condition)
       {
           /* Selection */
           FD_ZERO(&input_set );   /* Empty the FD Set */
           FD_SET(0, &input_set);  /* Listen to the input descriptor */
           ready_for_reading = select(1, &input_set, NULL, NULL, &timeout);
    
           /* Selection handling */
           if (ready_for_reading)
           {
              /* Do something clever with the input */
           }
           else
           {
              /* Handle the error */
           }
    
           /* Test the breaking condition */
           break_condition = some_calculation();
       }
       return 0;
    }
    

    Note that you have to have keep resetting the selection inside the loop so that it will respond again in the next iteration.

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

Sidebar

Related Questions

#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
#include <unistd.h> #include <stdio.h> #include <string.h> #include <sys/sendfile.h> #include <sys/stat.h> #include <fcntl.h> int main(){
This is my chat server : #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h>
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
When I try to use exec: #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h>
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <vector> #include <string> #include <iostream> FILE* fp;
Here is my code which checks if the file exists : #include<stdio.h> #include<zlib.h> #include<unistd.h>

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.