Misplaced Pages

Getc: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 18:19, 22 October 2011 editGene93k (talk | contribs)Autopatrolled, Extended confirmed users, New page reviewers, Pending changes reviewers468,116 editsm +Category:C standard library← Previous edit Revision as of 17:20, 29 October 2011 edit undo1exec1 (talk | contribs)Pending changes reviewers, Rollbackers50,085 edits fails WP:NOTMANUAL, WP:GNG and WP:ORNext edit →
Line 1: Line 1:
#redirect ]
{{merge to|C_file_input/output|date=October 2011}}
'''getc''' is one of the character input function. getc reads next character from file and it needs file pointer to tell it which file. It is simplest function to read the file.

Like ], getc() may be implemented macro instead of function. getc is equivalent to ].
getc returns the next character from the stream referred to by fp; it returns EOF for End Of File or error.

==Syntax==
int getc( FILE * stream);
Here, parameter stream is the pointer to a FILE object which identify the stream on which the operation is to be performed.

==Example==
/*getc example*/
<Source lang="c">
#include <stdio.h>
int main()
{
FILE *fp;
int c;
int n = 0;
fp = fopen("myfile.txt", "r");
if(fp == NULL)
perror ("Error opening file");
else
{
do {
c = getc(fp);
if(c == '#')
n++;
}
while(c != EOF);
fclose(fp);
printf ("File contains %d#.\n",n);
}
return 0;
}
</source>

Above program read the file called myfile.txt character by character and uses n variable to count '#' character contained in file.

==Return Value==
Character read is returned as an int value.

If the End-of-File is reached or error in reading occurs, function returns EOF and corresponding error indicator. We can use either ] or ] to determine whether an error happened or EOF reached.

==See also==
]

]

]

==Reference==
*
*

==External links==
*

]

Revision as of 17:20, 29 October 2011

Redirect to:

Getc: Difference between revisions Add topic