Javatpoint Logo
Javatpoint Logo

feof() function in C

In this article, we will discuss feof() function with its syntax, example, advantages, and disadvantages.

When conducting file input operations in C, the feof() function is utilized to determine if the user has reached at the end of a file. It is frequently used with file-handling functions like fscanf() and fgetc() and is specified in the standard input/output library (stdio. h).

The feof() function determines if the file's end has been reached. If the end-of-file indicator is set, it accepts a FILE pointer as an input and returns a non-zero value signifying the file's end.

Syntax:

The following is the syntax of the feof() function:

Each part of the definition stands for as follows:

int: An integer describes the feof() function's return type. If the give-up-of-report indication is set, it returns a fee that isn't 0, signifying that the file's stop has been reached. If not, it returns zero.

feof(): It is the function's name.

FILE *stream: It is the function's parameter. It is a pointer to a FILE shape, which determines how file streams are dealt with in C. The reported movement that you need to test for a cease-of-file situation is represented via the movement parameter.

Algorithm:

Here's the algorithm for the code you supplied, explained step by step:

  • First, include the important header document stdio.h for input/output
  • Define the main() function as the start line of the program.
  • Declare a report pointer variable report of type FILE*.
  • Use fopen() to open the file named "example.txt" in read mode. Assign the result to the report pointer.
  • Check if the record pointer is NULL, indicating that the report beginning operation failed.
  • If the record is NULL, show the mistake message "Failed to open the document." using printf().
  • Return 1 to signify an error happened and terminate this system.
  • After that, declare an integer variable person to shop each individual examine from the document.
  • Enter a while loop that continues so long as the person's study from the report isn't always the same as the EOF constant.
  • Use fgetc() function to examine the subsequent character from the report using the report pointer.
  • Assign the examined character to the individual variable.
  • Use putchar() to display the individual on the console.
  • After the while loop, use feof() to test if the end-of-file indicator is set for the file pointer.
  • If feof() returns a non-0 value (true), display the message "End of the report reached." with the help of printf() function.
  • If feof() returns zero (false), display the message "End of record not reached." the usage of printf().
  • Use fclose() to close the record circulation associated with the report pointer.
  • Return zero to indicate the successful execution of the program.

Program:

Output:

Failed to open the file.

Complexity Analysis:

Time Complexity:

The time complexity of the code depends on the scale of the report being examined. Let's anticipate the file includes N characters.

Opening the document (fopen()) and last the document (fclose()) operations have a steady time complexity and do not depend upon the record size. They may be considered O(1). The event loop iterates till the give-up of the file is reached, studying and displaying each individual. This loop's time complexity is O(N) because it performs N iterations, where N is the number of characters within the document.

The feof() function has a time complexity of O(1) because it tests the quit-of-report indicator reputation. Therefore, the general time complexity may be considered O(N), where N is the dimensions of the report.

Space Complexity:

The space complexity of the code is determined through the memory used to save variables and statistics systems. It remains notably steady and no longer relies upon the document size.

The space complexity of variable declarations (file, individual) and function parameters is O(1), considering that they require a set quantity of memory.

The memory required to hold the file content being read is small since the file is processed character by character rather than having the complete file content stored in memory.

Hence, the space complexity may be considered O(1), indicating regular reminiscence usage at some stage in the program execution, no matter the document length.

Characteristics of feof() in C:

Here are the characteristics of the feof() function in more detail:

End-of-File Indicator: The feof() function assessments the end-of-record indicator for a given document circulation. This indicator is a flag that is set through input features.

Once they attain the top of a report at some point in examination operations, no extra characters are left to be read from the report.

File Stream Check: The feof() function operates on a FILE* pointer representing a report move. A record move has usually created the usage of features like fopen(), reopen (), or open (). The FILE shape associated with the file movement incorporates facts about the record, such as the quit-of-file indicator.

Return Value: The feof() characteristic returns an integer cost. It returns a non-0 value (true) if the end-of-file indicator is about for the required report flow, indicating that the cease of the file has been reached. If the stop-of-report indicator is not set, it returns zero (false).

Usage: The feof() is often used together with file input operations. After acting an input operation, consisting of reading a character with fgetc() or studying formatted information with fscanf(), feof() may be known to determine if the give up of the file has been reached.

Loop Termination Condition: The feof() is commonly used in loops that study characters or records from a report till the end of the file is reached. By checking the end-of-file indicator with feof() within the loop condition, the loop can terminate without extra characters to examine.

Caution: It's essential to notice that feof() most effectively checks the status of the end-of-file indicator. It does not expect whether the next input operation will come across the cease of the document. Therefore, it is generally used after an input operation to decide if the stop of the record has been reached inside the modern-day new release.

Per-Stream Basis: The feof() function operates on an in-line with-flow foundation. Each file circulates its very own independent end-of-file indicator. Therefore, checking feof() for one circulate does not affect the end-of-file indicator of any other flow.

Input Operations Only: The feof() characteristic is designed to work with input operations. It isn't always intended to test the end-of-file repute for output operations. The feof() characteristic will not provide correct outcomes for output streams.

Unreliable for Predicting Future Input: The feof() function offers the most straightforward checks if the end-of-file indicator is set at the contemporary function in the file. It does not expect the next input operation to reach the top of the document. Therefore, it should not be used to anticipate the end-of-file condition before performing an enter operation.

Combination with Other Error Checks: The feof() characteristic is frequently utilized in aggregate with different error checks to handle record input operations. For example, further to checking feof(), it is not unusual to check for read errors using the error () feature to come across and handle any capability mistakes that can arise for the duration of record studying.

Advantages of feof() in C:

There are various advantages of the feof() function in C. Some main advantages of the feof() function are as follows:

End-of-File Detection:

  • feof() function affords a dependable mechanism to come across the end-of-file at some point of entering operations. It allows you to decide if you have reached the cease of the available statistics in a record.
  • It prevents reading past the end of the file, which may cause errors or surprising behavior in your software.

Loop Termination:

  • By using feof() in a loop condition, you may, without difficulty, create a loop that keeps until the end of the document is reached.
  • It simplifies studying and processing information from a document, as you can depend on the end-of-file indicator to terminate the loop while all the contents have been processed.

Platform Compatibility:

  • The feof() characteristic is part of the usual C library, making it universally available throughout one-of-a-kind structures and implementations.
  • It ensures that your code will paint continuously using the feof() function, no matter the underlying system.

Simplicity and Readability:

  • The feof() gives an honest and readable manner to test for the end-of-file circumstance for your code.
  • Using feof() at the side of loops, you could create easy and concise code that honestly expresses the rationale of studying facts until the document's give-up is reached.

Efficient File Processing:

  • Utilizing feof() within a loop, you may efficiently process big files without examining the complete record into reminiscence.
  • The iterative method lets you address documents of arbitrary sizes while ingesting sources handiest for a single character or information item at a time.

Compatibility with Other File Input Functions:

  • feof() is designed to work seamlessly with different file entry capabilities within the C library, which includes fgetc() and fscanf().
  • It integrates nicely into the existing file-enter workflow without problems, permitting you to include quit-of-file checking into your code.

Efficient Resource Management:

  • By using the feof() in conjunction with fclose(), you can ensure proper advantageous resource cleanup via ultimate document flow while the quit of the document is reached.
  • It prevents aid leaks and guarantees efficient control of system sources even when operating with files.

Disadvantages of feof() in C:

There are various disadvantages of the feof() function in C. Some main disadvantages of the feof() function are as follows:

Mixing with Other Errors:

  • While feof() is beneficial for checking the end-of-file circumstance, it has to be now not used as the only mechanism for managing all errors during record input operations.
  • Combining feof() with other mistakes assessments, including using error (), is usually recommended to discover and take care of any examination mistakes that could arise.
  • Using additional error assessments gives a more comprehensive approach to mistake management and ensures the reliability of your report input operations.

Order of Evaluation Limitation:

  • The feof() characteristic evaluates the end-of-file indicator on the present-day function inside the file stream. It should be known after a read operation as it should test the end-of-file repute.
  • If feof() is called earlier than studying from the report, it can no longer provide the appropriate result because the end-of-file indicator is ready simplest after an unsuccessful study operation.

Delayed Detection of End-of-File:

  • Only when a read operation has finished reading the entire file may feof() function identify the end-of-file condition.
  • After feof() function returns true, more read operations might be required to precisely identify the end-of-file condition.
  • The end-of-file condition may not be detected right away as a result of this delay in detection.

Misinterpretation of Return Value:

  • If the end-of-file indicator is set, feof() returns true; otherwise, it returns false.
  • feof() does not ensure that the following input action will reach the file's end, though.
  • It is possible to misread and make inaccurate assumptions about the file's content if you only rely on the return value of feof() without taking other aspects into account.

Dependence on Correct Read Operation:

  • The end-of-file situation is precisely determined by feof(), which depends on a successful read operation.
  • The output of feof() might not be accurate if the read operation meets an error or fails for any other reason.
  • Read errors must be handled individually using mechanisms like ferror to provide proper error detection and treatment.

Limited Usefulness for Binary Files:

  • The primary purpose of feof() is to perform text-based file input operations.
  • Their usefulness could be constrained when working with binary files that include non-textual data.
  • The end of relevant data in a binary file may not be reliably determined when relying simply on the end-of-file indicator offered by feof().
  • Extra methods or metadata may be needed to properly identify the end of the desired data structure in binary files.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA