The scanner waits till we enter 100 bytes of data. So if we are redirecting a file into the
executable's input, if the file has > 100 bytes of data. I scan it at one go, rather than line by line with fgets()
or scanf("%s")
etc.
Asked
Active
Viewed 166 times
0

Keith Thompson
- 254,901
- 44
- 429
- 631

sharky
- 327
- 4
- 13
-
3fread() is the function you are looking for. – Kylo Dec 10 '12 at 20:01
-
Related: Related: http://stackoverflow.com/q/8589425/827263 – Keith Thompson Dec 19 '14 at 22:15
1 Answers
3
You can use fread
to read the number of bytes you want, independent of line breaks or other whitespcae:
char buf[100];
size_t bytes_read = fread(buf, 1, 100, stdin);
Note that buf
will not be null-terminated. So if you want to printf
it, for instance (it needs a null terminated string), you can try the following:
char buf[101];
size_t bytes_read = fread(buf, 1, 100, stdin);
buf[100] = '\0'; // The 101th "cell" of buf will be
// the one at index `100` since the
// first one is at index `0`.

7heo.tk
- 1,074
- 12
- 23

Yakov Galka
- 70,775
- 16
- 139
- 220