{"id":3572,"date":"2024-03-06T11:33:59","date_gmt":"2024-03-06T11:33:59","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3572"},"modified":"2024-03-06T11:33:59","modified_gmt":"2024-03-06T11:33:59","slug":"eof-getc-and-feof-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/eof-getc-and-feof-in-c\/","title":{"rendered":"EOF, Getc() And Feof() In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#eof-getc-and-feof-in-c\">EOF, Getc() And Feof() In C<\/a><\/li><li ><a href=\"#getc-function-in-c\">getc() Function in C<\/a><\/li><li ><a href=\"#syntax-of-getc\">Syntax of getc()<\/a><\/li><li ><a href=\"#feof-function-in-c\">feof() Function in C<\/a><\/li><li ><a href=\"#example-of-feof\">Example of feof()<\/a><\/li><li ><a href=\"#faq-eof-getc-and-feof-in-c\">FAQ- EOF, getc() and feof() in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"eof-getc-and-feof-in-c\">EOF, Getc() And Feof() In C<\/h2>\n\n\n\n<p>In C\/C++, the <code>getc()<\/code> function is used to read a character from a file, and it returns the End of File (EOF) value when the end of the file is reached or if it encounters an error. However, relying solely on comparing the return value of <code>getc()<\/code> with EOF may not be sufficient to accurately determine if the end of the file has been reached. To address this issue, C provides the <code>feof()<\/code> function. The <code>feof()<\/code> function returns a non-zero value only when the end of the file has been reached, indicating that there are no more characters to read. In contrast, it returns 0 if the end of the file has not been reached. By using <code>feof()<\/code> in conjunction with <code>getc()<\/code>, developers can more reliably determine whether they have reached the end of a file while handling potential error conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getc-function-in-c\">getc() Function in C<\/h2>\n\n\n\n<p>The <code>getc()<\/code> function in C is commonly used to read a single character from the specified file stream. It is implemented as a macro in the <code>&lt;stdio.h&gt;<\/code> header file. The <code>getc()<\/code> macro takes a file pointer as an argument, reads the next character from the associated stream, and returns it as an unsigned char cast to an int. If the end of the file is encountered or an error occurs, it returns the special value EOF (End of File). This function is often used in conjunction with other file input\/output functions to process data from files in C programming. It&#8217;s important to note that <code>getc()<\/code> is essentially equivalent to the <code>fgetc()<\/code> function, and in most implementations, <code>getc()<\/code> is defined as a macro that expands to a call to <code>fgetc()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-getc\">Syntax of getc()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int getc(FILE* stream);\n<\/code><\/pre>\n\n\n\n<p><strong>Parameters<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>stream<\/strong>: It is  referrred as a pointer to a file stream to read the data from.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Value<\/strong><\/p>\n\n\n\n<p> The <code>getc()<\/code> function in C returns the character read from the file stream as an unsigned char cast to an int. If no error occurs and the End-Of-File (EOF) is not reached, it returns the character read. However, in case of an error or when the End-Of-File is encountered, it returns the special value <code>EOF<\/code>. This is a crucial aspect of handling file operations in C, allowing developers to check for the end of the file or errors during file reading. Thank you for highlighting this important point.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"feof-function-in-c\">feof() Function in C<\/h2>\n\n\n\n<p>The <code>feof()<\/code> function in C is employed to determine whether the file pointer associated with a stream has reached the end of the file. When called, <code>feof()<\/code> returns a non-zero value if the end of the file has been reached, indicating that there are no more characters to read from the file. Conversely, if the end of the file has not been reached, it returns 0. This function is commonly used in file-handling scenarios to conditionally check whether further attempts to read from the file should be made based on the status of the file pointer. It&#8217;s a useful tool in avoiding attempting to read beyond the end of a file.<\/p>\n\n\n\n<p>Syntax of feof()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int feof(FILE* stream);\n<\/code><\/pre>\n\n\n\n<p><strong>Parameters<\/strong><\/p>\n\n\n\n<p>It  will returns the character read from the file stream.<\/p>\n\n\n\n<p><strong>Return Value<\/strong><\/p>\n\n\n\n<p>Whereas, If the end-of-file indicator is set for the stream, then it means that the function will return as  a non-zero value (usually 1). Otherwise, it returns 0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-feof\"><strong>Example of feof()<\/strong><\/h2>\n\n\n\n<p>In the program, the <code>getc()<\/code> function is used to read a character from a file, and its return value is first checked for equality with <code>EOF<\/code>. This check helps determine if the end of the file has been reached or if an error occurred during reading. Subsequently, the program employs the <code>feof()<\/code> function to further confirm the end-of-file condition. If <code>feof()<\/code> returns a non-zero value, it signifies the actual end of the file, and the program prints &#8220;End of file reached.&#8221; On the other hand, if <code>feof()<\/code> returns 0, indicating that the <code>EOF<\/code> from <code>getc()<\/code> was due to an error, the program prints &#8220;Something went wrong.&#8221; This two-step verification ensures accurate handling of end-of-file situations and proper error management during file reading in the C program.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate the use of getc(), feof() and\n\/\/ EOF\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ Open the file \"test.txt\" in read mode\n    FILE* fp = fopen(\"test.txt\", \"r\");\n    \/\/ Read the first character from the file\n    int ch = getc(fp);\n \n    \/\/ Loop until the end of the file is\n    \/\/ reached\n    while (ch != EOF) {\n        \/* display contents of file on screen *\/\n        putchar(ch);\n \n        \/\/ Read the next character from the file\n        ch = getc(fp);\n    }\n \/\/ Check if the end-of-file indicator is\n    \/\/ set for the file\n    if (feof(fp))\n        printf(\"\\n End of file reached.\");\n    else\n        printf(\"\\n Something went wrong.\");\n \n    \/\/ Close the file\n    fclose(fp);\n \n    \/\/ Wait for a keypress\n    getchar();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Consider the  file \u201ctest.txt\u201d has  the following data:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-eof-getc-and-feof-in-c\">FAQ- EOF, getc() and feof() in C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700125669537\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the use of feof () function in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>feof()<\/code> function in C indicates whether the end-of-file flag is set for a specific stream. This flag is raised by certain functions when the end of the file is reached. To reset or clear this flag, functions like <code>rewind()<\/code>, <code>fsetpos()<\/code>, <code>fseek()<\/code>, or <code>clearerr()<\/code> can be used on the respective stream. These operations are essential for managing file-related operations and ensuring accurate file handling in C programs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700125678099\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What is the difference between EOF and feof functions in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In short, <code>EOF<\/code> is a constant used to compare against return values (e.g., from <code>getc()<\/code>) to check for the end of a stream. On the other hand, <code>feof()<\/code> is a function that directly checks if a file pointer has reached the end of the associated file.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700125682199\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What is Getc () in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>getc()<\/code> function in C is utilized to read a single character from a specified file stream. Implemented as a macro in the <code>&lt;stdio.h><\/code> header file, it plays a crucial role in file input operations in C programming.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>EOF, Getc() And Feof() In C In C\/C++, the getc() function is used to read a character from a file, and it returns the End of File (EOF) value when the end of the file is reached or if it encounters an error. However, relying solely on comparing the return value of getc() with EOF &#8230; <a title=\"EOF, Getc() And Feof() In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/eof-getc-and-feof-in-c\/\" aria-label=\"More on EOF, Getc() And Feof() In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5422,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[576,577],"class_list":["post-3572","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-eof","tag-getc-and-feof-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3572"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3572\/revisions"}],"predecessor-version":[{"id":7945,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3572\/revisions\/7945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5422"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}