I'd like to make 2 small but important points here.
First, it's a bad idea to specify the string length when initializing an array with a string literal, Example:
Code:
char ar[5] = "Hello George";
Note how 5 is not enough to hold the array, this is bad. That's why you should always declare it like this unless you are declaring a buffer of a certain
size in which case you probably want to initialize to 0 not a string literal:
Code:
char ar[] = "Hello George"; /* compiler determines length automatically */
/* a generic buffer */
char buf[512] = {0}; /* this ensures that if i only fill lets say 10 bytes of this buffer, the rest of the buffer will always contain 0s and thus be a valid string */
Second point, when using fgets you do not need to specify SIZE-1, fgets already does this for you and it simply results in odd problems (if you are for example using an exact size, which is quite often the case in my experience) or simply harder to read for no reason what-so-ever. Here is the preferred method again:
Code:
#define MAX_SIZE 100
char ar[MAX_SIZE];
...
fgets(ar, MAX_SIZE, fp);
If you don't believe me, here's the excerpt from the fgets man page:
fgets() reads in at most
one less than size characters from stream and stores them into the buffer pointed to by
s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating
null byte ('\0') is stored after the last character in the buffer.