File or Console Input

File or Console Input

Hard C Control Flow 31 views
Explanation Complexity

Problem Statement

As soon as the program starts, check whether the input is coming from a file (redirected) or from the keyboard. If it is from a file, then give a "Reading from file..." message, otherwise give an "Enter data:" prompt.

Input Format

Input may come either from keyboard (stdin) or from a redirected file.


Output Format

A message indicating the input source:

• "Reading from file..."

• "Enter data:"

Example

Input redirected from a file
OR
User types input from keyboard


Reading from file...
OR
Enter data:

Constraints

• Program runs in C language

• Uses standard input (stdin)

Concept Explanation

In C, both keyboard input and file-redirected input are read using stdin.
The program must check whether stdin is connected to a terminal (keyboard) or not.

Step-by-Step Explanation

1.At the start of the program, check the input source.

2.Use a system check to determine whether stdin is connected to a terminal.

3.If stdin is not connected to a terminal:

• Input is coming from a file (redirection).

• Print "Reading from file...".

4.If stdin is connected to a terminal:

• Input is coming from the keyboard.

• Print "Enter data:".

5.After printing the message, continue normal input processing.

Concept Explanation

In C, both keyboard input and file-redirected input are read using stdin.
The program must check whether stdin is connected to a terminal (keyboard) or not.

Step-by-Step Explanation

1.At the start of the program, check the input source.

2.Use a system check to determine whether stdin is connected to a terminal.

3.If stdin is not connected to a terminal:

• Input is coming from a file (redirection).

• Print "Reading from file...".

4.If stdin is connected to a terminal:

• Input is coming from the keyboard.

• Print "Enter data:".

5.After printing the message, continue normal input processing.

Input / Output Format

Input Format
Input may come either from keyboard (stdin) or from a redirected file.


Output Format
A message indicating the input source:

• "Reading from file..."

• "Enter data:"
Constraints
• Program runs in C language

• Uses standard input (stdin)

Examples

Input:
Input redirected from a file OR User types input from keyboard
Output:
Reading from file... OR Enter data:

Example Solution (Public)

C
#include <stdio.h>
#include <unistd.h>   // for isatty()

int main() {

    if (isatty(fileno(stdin))) {
        // Input is from keyboard
        printf("Enter data:n");
    } else {
        // Input is redirected from a file or pipe
        printf("Reading from file...n");
    }

    /* Example: read and echo input */
    char buffer[100];
    while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
        printf("%s", buffer);
    }

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <unistd.h>   // for isatty()

int main() {

    if (isatty(fileno(stdin))) {
        // Input is from keyboard
        printf("Enter data:n");
    } else {
        // Input is redirected from a file or pipe
        printf("Reading from file...n");
    }

    /* Example: read and echo input */
    char buffer[100];
    while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
        printf("%s", buffer);
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.