Skip to main content
Qye
Associate II
October 11, 2023
Question

STM fgets character limit

  • October 11, 2023
  • 2 replies
  • 1710 views

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;
}

This topic has been closed for replies.

2 replies

Qye
QyeAuthor
Associate II
October 11, 2023

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
QyeAuthor
Associate II
October 11, 2023

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