Open In App

How to Read Input Until EOF in C?

Last Updated : 03 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, reading input until the End of the File (EOF) involves reading input until it reaches the end i.e. end of file. In this article, we will learn various methods through which we can read inputs until EOF in C.

Read Input Until EOF Using getchar()

The getchar() method is a built in method provided in C which reads one character at a time from the standard input. The getchar() function returns the character read as unsigned char cast to an integer or EOF on reaching end of the file.

Syntax

int getchar(void)

getchar() function does not take any parameters and returns EOF if the end of the file is reached or user terminated the execution.

C program to Read Input Until EOF in C using getchar() function

The following program illustrates how we can read input until EOF in C using getchar() function.

C
// C program to read input until EOF using getchar()
#include <stdio.h>

int main()
{
    // Variable to store each character read
    int ch;

    printf("Enter text\nPress (Ctrl+Z to terminate the "
           "loop):\n");

    // Read characters until EOF is encountered
    while ((ch = getchar()) != EOF) {
        // Print each character as it's read
        if (ch != '\n') {
            printf("You entered: %c\n",
                   ch); // Print the character entered
        }
    }

    printf("\nEOF reached. Program terminated.\n");

    return 0;
}


Output

Enter text
Press (Ctrl+D on Unix or Ctrl+Z on Windows to terminate the loop):
G
You entered: G
E
You entered: E
E
You entered: E
K
You entered: K
S
You entered: S

EOF reached. Program terminated.

Time Complexity:O(N), where N is the number of inputs.
Auxiliary Space: O(1)

Read Input Until EOF Using fgets()

The fgets() function in C is used to read a line of a text at a time using a fixed size buffer. The fgets() function reads up to buffersize-1 characters from the standard input and stores them in the buffer. It stops it's execution whenever it encounters a newline character or EOF, thus it can be used to read input until EOF in C.

Syntax

char *fgets(char *str, int n, FILE *buffer);

Here,

  • str: Pointer to an array of chars where the string is stored.
  • n: Denotes the maximum number of characters to be read (including the null character).
  • buffer: Pointer to a FILE object that identifies the stream to read from.
  • Return value: Returns the read str on success and on failure returns EOF.

C program to Read Input Until EOF in C using fgets() function

The following program illustrates how we can read input until EOF in C using fgets() function.

C
// C program to read input until EOF using fgets()
#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 1000 // Maximum length of input line

int main()
{
    // Buffer to store each line
    char line[MAX_LENGTH];
    // Counter for number of lines read
    int line_count = 0;

    printf("Enter text (Press Ctrl+D on Unix or Ctrl+Z on "
           "Windows for EOF):\n");

    // Read lines until EOF is encountered
    while (fgets(line, MAX_LENGTH, stdin) != NULL) {
        line_count++;
        // Remove the newline character at the end of the
        // line, if it exists
        line[strcspn(line, "\n")] = 0;
        // Print each line with its line number
        printf("You entered: Line %d: %s\n", line_count,
               line);
    }

    printf("\nEOF reached. Total lines read: %d\n",
           line_count);

    return 0;
}


Output

Enter text (Press Ctrl+D on Unix or Ctrl+Z on Windows for EOF):
Hello Geeks
You entered: Line 1: Hello Geeks
This is a tutorial for
You entered: Line 2: This is a tutorial for
Reading inputs in C
You entered: Line 3: Reading inputs in C
Until EOF is encountered
You entered: Line 4: Until EOF is encountered

EOF reached. Total lines read: 4

Time Complexity: O(N), where N is the number of inputs.
Auxiliary Space: O(M), where M is the size of the buffer.

Read Input Until EOF Using scanf()

The scanf() method can also be used to read inputs until EOF in C. Scanf automatically terminates the execution of the program whenever it encounters an EOF. Followis is the syntax to use scanf() function:

Syntax

int scanf(const char *format, ...);

Here,

  • format: It is a C string that contains the format specifers.
  • return value: Number of input items successfully matched and assigned. Returns EOF if an error occurs or end of input is reached before any data is successfully read.

C program to Read Input Until EOF in C using scanf() function

The following program illustrates how we can read input until EOF in C using scanf() function.

C
// C program to read input until EOF using scanf()
#include <stdio.h>
#include <string.h>
// Maximum length of input line
#define MAX_LENGTH 1000

#include <stdio.h>

int main()
{
    // Variable to store each number read
    int num;
    // Counter for number of integers read
    int count = 0;

    printf("Enter your inputs or (PressCtrl+D on Unix or "
           "Ctrl+Z on Windows for EOF):\n");

    // Read integers until EOF is encountered
    while (scanf("%d", &num) != EOF) {
        count++;
        // Print each number with its count
        printf("You entered: Number %d: %d\n", count, num);
    }

    printf("\nEOF reached. Total numbers read: %d\n",
           count);

    return 0;
}


Output

Enter your inputs or (PressCtrl+D on Unix or Ctrl+Z on Windows for EOF):
10
You entered: Number 1: 10
20
You entered: Number 2: 20
30
You entered: Number 3: 30
40
You entered: Number 4: 40
50
You entered: Number 5: 50

EOF reached. Total numbers read: 5

Time Complexity:O(N), where N is the number of inputs.
Auxiliary Space: O(1)


Next Article

Similar Reads