0

I wanted to know how to deal with a situation when I set the size of buffer in fgets to n bytes and the user enters 2n bytes? fgets will reaf the first n bytes, and the other n bytes will be left in stdin, right? Is it a good idea to flush stdin after each fgets, is there any other optiob to deal with that situation?

Keselme
  • 3,779
  • 7
  • 36
  • 68
  • 2
    don't flush stdin, it's undefined behaviour. Rather, use a fgetc() loop until EOF. – Jean-François Fabre Jul 30 '17 at 19:13
  • @Jean-FrançoisFabre EOL presumably... – Antti Haapala -- Слава Україні Jul 30 '17 at 19:20
  • It depends on what you want to do? discard rest of the line? resize your input buffer? – Antti Haapala -- Слава Україні Jul 30 '17 at 19:21
  • In this situation, I usually grow the input buffer (e.g., double the size using realloc) and continue to read. And yes, the remaining bytes remain there in stdin and can be read by subsequent fgets calls. – Leon Avery Jul 30 '17 at 19:22
  • @AnttiHaapala probably. Too lazy to look that up. Congrats for the c gold badge BTW. Maybe you could find a good dupe of that one and close it :) – Jean-François Fabre Jul 30 '17 at 19:25
  • Note the options discussed in [Why the `gets()` function is so dangerous it should never be used](https://stackoverflow.com/a/4309845/15168). My normal technique is to set the size large (4096 bytes, typically) and then largely ignore the problem — [GIGO](https://en.wikipedia.org/wiki/Garbage_in,_garbage_out). But I do mean that size of buffer. OTOH, you might be parsing JSON-style data that could all be on one line and might be larger. In that case, `getline()` is good, or you can use `fgets()` in a loop resizing the array as you go (but `getline()` does that automatically). – Jonathan Leffler Jul 30 '17 at 20:16
  • 1
    A good answer needs to address the higher level goals of the program - which are not stated. Append the goals of the program to get good applicable ideas -else this is too broad. – chux - Reinstate Monica Jul 30 '17 at 20:19
  • @Jean-FrançoisFabre it's fine to flush `stdin`. Just not with the `fflush` function – M.M Jul 30 '17 at 21:11
  • Hey, thanks for the answers everyone, I want to clarify what I mean. For instance I want to user to enter a name and give him 20 bytes for it, but he decides to enter 30, I don't want the extra 10 bytes, so I can either flush it or I can provide a bigger container. In a case of the providing bigger container size the user can always enter an input longer than the container size, so if I provide 4096 bytes, he can enter 5000 and so on. So, I guess my question is how to deal with it? – Keselme Jul 31 '17 at 04:01

1 Answers1

0

You know when the input was truncated by the absence of a '\n' newline at the end of the input string.

Except possibly for the last line in a text file, which might not contain any newline.

If there is more input, you can realloc the buffer (originally acquired with malloc) and repeat with the appropriate buffer pointer (and size) to append to what you already have.

No need to flush anything, just pass the right pointer and additional buffer size to fgets.

In any case, flushing input with fflush() is not well defined.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56