cancel
Showing results for 
Search instead for 
Did you mean: 

STM fgets character limit

Qye
Associate II

fgets ignores my limit and is trying to read 1024 characters.

char *fgets( char *str, int numChars, FILE *stream );

as I understand fgets it is supposed to read up to numCHars characters from stream into str. It should also stop on a newline

The behavior I see is that changes the numChars in __srefill_r to 1024. I also see no code in the disassembly to deal with \n. By the time _read is called the len argument is 1024. Is stdin a block mode device?

Do I misunderstand fgets or what might be happening here.

I overrode __io_getchar as follows. It is being hit and does return the chars I type. 

int __io_getchar(void)
{
uint8_t ch = '\n';

HAL_StatusTypeDef eStatus;

eStatus = HAL_UART_Receive(&huart3, &ch, 1, HAL_MAX_DELAY);

return ch;
}

2 REPLIES 2
Qye
Associate II

I am using this in its place

int getline(char *ptr, int len)
{
    int DataIdx;

    for (DataIdx = 0; DataIdx < len; DataIdx++)
    {
        *ptr = __io_getchar();
        if (*ptr == '\n' )
        {
            *ptr = '\0';
            break;
        }
        ptr++;
    }
    return len;
}
 
Qye
Associate II

I wrote my own getline since I didnt find it in the libraries