fgetc

*Might* an unsigned char be equal to EOF? [duplicate]

我们两清 提交于 2021-02-07 05:44:13
问题 This question already has answers here : Can sizeof(int) ever be 1 on a hosted implementation? (8 answers) Closed 5 years ago . When using fgetc to read the next character of a stream, you usually check that the end-of-file was not attained by if ((c = fgetc (stream)) != EOF) where c is of int type. Then, either the end-of-file has been attained and the condition will fail, or c shall be an unsigned char converted to int , which is expected to be different from EOF —for EOF is ensured to be

Counting number of lines in the file in C

余生长醉 提交于 2021-02-05 07:32:05
问题 I'm writing a function that reads the number of lines in the given line. Some text files may not end with a newline character. int line_count(const char *filename) { int ch = 0; int count = 0; FILE *fileHandle; if ((fileHandle = fopen(filename, "r")) == NULL) { return -1; } do { ch = fgetc(fileHandle); if ( ch == '\n') count++; } while (ch != EOF); fclose(fileHandle); return count; } Now the function doesn't count the number of lines correctly, but I can't figure out where the problem is. I

How to read characters from a file until a space and store that as one string (c)?

对着背影说爱祢 提交于 2020-06-17 04:11:02
问题 I need to read all the information from a line in a file before a space and store it as a string, how do I do that? File example: Biology A 4.0 Tennis B 1.0 etc (I need the letter and number later and I know how to store them fine, just the dang string is giving me trouble) So far I have int main (void) { FILE* grades; char className[10]; char currChar; int i = 0; grades = fopen("grades.txt", "r"); if (fgetc(grades) != ' ') { currChar = fgetc(grades); className[i] = currChar; i++; } /* what I

How to read characters from a file until a space and store that as one string (c)?

独自空忆成欢 提交于 2020-06-17 04:10:07
问题 I need to read all the information from a line in a file before a space and store it as a string, how do I do that? File example: Biology A 4.0 Tennis B 1.0 etc (I need the letter and number later and I know how to store them fine, just the dang string is giving me trouble) So far I have int main (void) { FILE* grades; char className[10]; char currChar; int i = 0; grades = fopen("grades.txt", "r"); if (fgetc(grades) != ' ') { currChar = fgetc(grades); className[i] = currChar; i++; } /* what I

fgetc not working C- returns same char repeatedly

人盡茶涼 提交于 2020-01-06 23:09:46
问题 I am working in C and trying to read a file and save the chars returned by fgetc in an array. The problem is that fgetc is returning a random char repeatedly. Marked location in comment. For example, "wwwwwwwwwwww..." or "@@@@@@@@@@@@...". Any help is appreciated. Here is the function: void removeCategories(FILE *category, int *prodPrinted){ char more[16] = { '\0' }, hidden[17] = { '\0' }, temp = '\0', mdn[] = { "More Data Needed" }, hnl[] = { "Hidden non listed" }; int newLineCount = 0, i,

fgetc not working C- returns same char repeatedly

巧了我就是萌 提交于 2020-01-06 23:08:09
问题 I am working in C and trying to read a file and save the chars returned by fgetc in an array. The problem is that fgetc is returning a random char repeatedly. Marked location in comment. For example, "wwwwwwwwwwww..." or "@@@@@@@@@@@@...". Any help is appreciated. Here is the function: void removeCategories(FILE *category, int *prodPrinted){ char more[16] = { '\0' }, hidden[17] = { '\0' }, temp = '\0', mdn[] = { "More Data Needed" }, hnl[] = { "Hidden non listed" }; int newLineCount = 0, i,