getchar returns one of the following: A character, represented as an unsigned char value (e.g. typically between 0 and 255, inclusive of those values), converted to an int. Thus, there are typically one of 256 (UCHAR_MAX+1, technically) values that fall into this category. A non-character, EOF, which has a negative...
The trouble you have is related to what fgetc returns. The return type is int but you are storing it into an unsigned char. You must either change this to be an int or, as an alternative, use feof to check for an end of file condition....
I suggest that you simply use fscanf. E.g FILE *file; int i = 0, status; float value; float rate[16]; file = fopen(myFile, "r"); if(file == NULL){ printf("Unable to read the file!\n"); return ; } while((status=fscanf(file, "%f", &value))!=EOF){ if(status==1){ rate[i++] = value; if(i==16)//full break; } else { fgetc(file);//one character drop }...
You are reading the newline (10) and the EOF return value of fgetc (-1). Substracting '0' (48) from those yields those negative numbers. Check if the char if valid, it must be in range ['0','9'] c1 = fgetc(fread); if(c1 >= '0' && c1 <= '9') { c1 -= '0'; //...
Here is code that seems to work: #include <assert.h> #include <ctype.h> #include <stdio.h> #include <string.h> enum { MAX_WORDS = 64, MAX_WORD_LEN = 20 }; int main(void) { char words[MAX_WORDS][MAX_WORD_LEN]; int count[MAX_WORDS] = { 0 }; int w = 0; char word[MAX_WORD_LEN]; int c; int l = 0; while ((c =...
/*Everything looks find in your code. in my machine it is working fine . I have only added a if condition to print the contents same as the file thats it .. */ #include<stdio.h> int main(void) { FILE *fp; int c; fp = fopen("rabi.txt","r"); if(fp == NULL) { perror("Error in...
If you are using open() and close(), you cannot use fgetc(). You need to use fopen() and fclose() to be able to use fgetc(). Either way, you need a function which can be called with either the standard input (spelled 0 or stdin) or with the file that was opened...
while(value =! EOF); should be while(value != EOF); It's a big difference. First case is an assignment to the variable value, second case looks if value is not equal to EOF. EOF is a macro and usually have the value -1, that means that !EOF becomes 0. Since you have...
When a file is opened for read and write, an fseek() is used when switching between operations. fseek( fp, 0, SEEK_CUR); does not change the position of the file pointer in the file. #include<stdio.h> #include<stdlib.h> int main ( ) { int read = 0; int write = 48; int each...
You said: What is giving me trouble is checking when they could either enter no characters, up to n amount, or too many. My suggestion: If you are expecting to see at most N characters, create an array whose size is larger than N+2. You need at least N+2 just...