c-embed icon indicating copy to clipboard operation
c-embed copied to clipboard

egets does not add the new-line if present and does not add the terminating null byte

Open gwiesenekker opened this issue 2 years ago • 1 comments

Also, I think it should scan for '\n' instead of '\r'.

Regards, GW

gwiesenekker avatar Dec 19 '22 07:12 gwiesenekker

I have changed egets to:

char* egets ( char* str, int num, EFILE* stream ){

  //if num 0 or 1 stream->pos will not advance
  assert(num > 1);
  if (eeof(stream) return NULL;
  
  int i = 0; 
  
  while(TRUE)
  {
    //i < (num - 1) so still room for two characters: \n and \0   
    if ((str[i++] = *(stream->pos++)) == '\n') break;
    if (eeof(stream) or (i == (num - 1))) break;
  }
  str[i] = '\0';
  return str;
}

gwiesenekker avatar Dec 27 '22 05:12 gwiesenekker