Hello,
gcc 4.4.4 c89
I am using the following code to read in file using fgets. I just want to get the gender which could be either M or F.
However, as the gender is always the last character in the string. I thought I could get the character by using strlen. However, for some reason I have to get the strlen and minus 2. I know that the strlen doesn't include the nul. However, it will include the carriage return.
The exact line of text I am reading in is this:
"Low, Lisa" 35 F
My code:
int read_char(FILE *fp)
{
#define STRING_SIZE 30
char temp[STRING_SIZE] = {0};
int len = 0;
fgets(temp, STRING_SIZE, fp);
if(temp == NULL) {
fprintf(stderr, "Text file corrupted\n");
return FALSE;
}
len = strlen(temp);
return temp[len - 2];
}
The strlen returns 17 when I feel it should return 16. String length including the carriage return. I feel I should be doing - 1 instead of - 2.
Any suggestions if you understand my question.
Thanks,
EDIT:
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 '\0' is stored after the last character in the
buffer
So the buffer will contain:
"Low, Lisa" 35 F\0\r
Which will return 17 from strlen if it is including the \r? Am I correct in thinking that?