fgetc

Counting words in a file in C

那年仲夏 提交于 2019-12-05 21:03:57
I'm writing a function that counts the number of words in a file. Words may be separated by any amount of whitespace characters. There can be integers in a file, but the program should only count words which have at least one alphabetic character. int word_count(const char *filename) { int ch; int state; int count = 0; FILE *fileHandle; if ((fileHandle = fopen(filename, "r")) == NULL){ return -1; } state = OUT; count = 0; while ((ch = fgetc(fileHandle)) != EOF){ if (isspace(ch)) state = OUT; else if (state == OUT){ state = IN; ++count; } } fclose(fileHandle); return count; } I figured out how

What is the better way to check EOF and error of fgetc()? [duplicate]

独自空忆成欢 提交于 2019-12-05 05:08:54
This question already has an answer here: Is it possible to confuse EOF with a normal byte value when using fgetc? 3 answers I always use this approach int c; while ((c = fgetc(fp))!=EOF) { printf("%c", c); } As it seems to me more readable and robust. But to an answer of mine link , chux commented that if ( feof(fp) ) is more robust than int c; while ((c = fgetc(fp))!=EOF) As while(1) { c = fgetc(fp); if ( feof(fp) ) { break ; } printf("%c", c); } is more robust than the first version. So what version should I use? Please explain me why that version is better. EDIT In question Why is “while (

Reading string by char till end of line C/C++ [duplicate]

本秂侑毒 提交于 2019-11-30 06:48:32
问题 This question already has answers here : c++ compile error: ISO C++ forbids comparison between pointer and integer (5 answers) Closed 2 years ago . How to read a string one char at the time, and stop when you reach end of line? I'am using fgetc function to read from file and put chars to array (latter will change array to malloc), but can't figure out how to stop when the end of line is reached Tried this (c is the variable with char from file): if(c=="\0") But it gives error that I cant

Is it possible to confuse EOF with a normal byte value when using fgetc?

坚强是说给别人听的谎言 提交于 2019-11-29 14:38:34
We often use fgetc like this: int c; while ((c = fgetc(file)) != EOF) { // do stuff } Theoretically, if a byte in the file has the value of EOF , this code is buggy - it will break the loop early and fail to process the whole file. Is this situation possible? As far as I understand, fgetc internally casts a byte read from the file to unsigned char and then to int , and returns it. This will work if the range of int is greater than that of unsigned char . What happens if it's not (probably then sizeof(int)=1 )? Will fgetc read a legitimate data equal to EOF from a file sometimes? Will it alter

How do i print escape characters as characters?

血红的双手。 提交于 2019-11-29 12:36:44
I'm trying to print escape characters as characters or strings using this code: while((c = fgetc(fp))!= EOF) { if(c == '\0') { printf(" \0"); } else if(c == '\a') { printf(" \a"); } else if(c == '\b') { printf(" \b"); } else if(c == '\f') { printf(" \f"); } else if(c == '\n') { printf(" \n"); } else if(c == '\r') { printf(" \r"); } else if(c == '\t') { printf(" \t"); } else if(c == '\v') { printf(" \v"); } } but when i try it, it actually prints the escape sequence. Escape the slashes (use " \\a" ) so they won't get interpreted specially. Also you might want to use a lookup table or a switch

C fgets versus fgetc for reading line

社会主义新天地 提交于 2019-11-29 07:48:33
问题 I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities: Use fgets and check each time if the last character is a newline and continuously append to a buffer Read each character using fgetc and occasionally realloc the buffer Intuition tells me the fgetc variant might be slower, but then again I don't see how fgets can do it without examining every character (also my intuition isn't always that good). The lines are

Reading string by char till end of line C/C++ [duplicate]

匆匆过客 提交于 2019-11-28 21:53:01
This question already has an answer here: c++ compile error: ISO C++ forbids comparison between pointer and integer 5 answers How to read a string one char at the time, and stop when you reach end of line? I'am using fgetc function to read from file and put chars to array (latter will change array to malloc), but can't figure out how to stop when the end of line is reached Tried this (c is the variable with char from file): if(c=="\0") But it gives error that I cant compare pointer to integer File looks like (the length of the words are unknown): one two three So here comes the questions: 1)

Is it possible to confuse EOF with a normal byte value when using fgetc?

泄露秘密 提交于 2019-11-28 08:33:11
问题 We often use fgetc like this: int c; while ((c = fgetc(file)) != EOF) { // do stuff } Theoretically, if a byte in the file has the value of EOF , this code is buggy - it will break the loop early and fail to process the whole file. Is this situation possible? As far as I understand, fgetc internally casts a byte read from the file to unsigned char and then to int , and returns it. This will work if the range of int is greater than that of unsigned char . What happens if it's not (probably

How do i print escape characters as characters?

前提是你 提交于 2019-11-28 06:27:21
问题 I'm trying to print escape characters as characters or strings using this code: while((c = fgetc(fp))!= EOF) { if(c == '\0') { printf(" \0"); } else if(c == '\a') { printf(" \a"); } else if(c == '\b') { printf(" \b"); } else if(c == '\f') { printf(" \f"); } else if(c == '\n') { printf(" \n"); } else if(c == '\r') { printf(" \r"); } else if(c == '\t') { printf(" \t"); } else if(c == '\v') { printf(" \v"); } } but when i try it, it actually prints the escape sequence. 回答1: Escape the slashes

fgetc does not identify EOF [duplicate]

。_饼干妹妹 提交于 2019-11-27 15:41:34
This question already has an answer here: Difference between int and char in getchar/fgetc and putchar/fputc? 2 answers The program below runs fine on various Solaris/Linux flavours, but not on AIX. However, if I replace while(c!=EOF) with while(c!=0xff) on AIX it runs completely fine. Any thoughts? I checked the fgetc man page on AIX, and it should return the EOF constant! #include <stdio.h> #include<unistd.h> #include <string.h> int main() { char c; FILE *fp; fp = fopen("a.txt", "r"); c=fgetc(fp); while(c!=EOF) { c=fgetc(fp); printf("%d",c); } fclose(fp); return 0; } The return value of