How to Read Input Until EOF in C?
Last Updated :
03 Jul, 2024
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.
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.
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)
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.
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.
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.
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)
Similar Reads
How to Read From a File in C? File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. In this article,
2 min read
How to Take Multiple Input in C? In C, we use scanf to take input from the user, and often it is necessary to take multiple inputs simultaneously. In this article, we will learn about how to take multiple inputs from the users in C. Multiple Inputs From a User in CTo take multiple inputs from users in C, we can declare an array to
2 min read
How to Read a File Line by Line in C? In C, reading a file line by line is a process that involves opening the file, reading its contents line by line until the end of the file is reached, processing each line as needed, and then closing the file.Reading a File Line by Line in CReading a file line by line is a step by step:1. Opening th
3 min read
How to Read Data Using sscanf() in C? In C, sscanf() function stands for "String Scan Formatted". This function is used for parsing the formatted strings, unlike scanf() that reads from the input stream, the sscanf() function extracts data from a string. In this article, we will learn how to read data using sscanf() function in C. Examp
2 min read
How to write in a file using fputs() in C fputs() is a function declared in stdio.h header file. It is used to write the contents of the file. The function takes 2 arguments. The first argument is a pointer to the string which is to be written and the second argument is the pointer of the file where the string is to be written. It returns 1
2 min read
Employee Record System in C using File Handling Employee Record System is software built to handle the primary housekeeping functions of a company. ERS helps companies keep track of all the employees and their records. It is used to manage the company using a computerized system. This software built to handle the records of employees of any compa
5 min read