Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

This thread hasn't been pruned or deleted.

Name: Anonymous 2017-02-26 0:49

In fact, it's brand new!

Name: Steve 2017-02-26 2:43

oh ok

Name: Anonymous 2017-02-26 3:54

So, since the front page of /prague/ currently seems insufficiently programming related, what's the best way to read a string containing whitespace from standard input in C?

scanf() stops scanning at the first whitespace character.
gets() is unsafe since it offers no protection against buffer overflows.
fgets() has neither of the problems of the above, but copies the newline into the buffer, meaning the string can't be printed unless you sanitize it by stripping out the newline first.
gets_s() seems not to have any of these problems, but wasn't introduced until C11.

Why wasn't there a simple safe string input function, that simply reads the first n characters (including whitespace) into a character buffer, in the C library from the beginning? It seems silly that they have such a variety of I/O functions, but none really suitable for such a simple task.

Also, what is even the purpose of the getc() function? It literally has the exact same behavior as fgetc(), since both take a file parameter, and furthermore, getc() goes against the convention that I/O functions only take a file parameter if they're the f-version (fprintf, fputs, etc), and I/O functions without an f prefix are assumed to work on stdin or stdout. So not only is getc() superflous, its name is misleading about how it is used. So why is it even included in the C standard library?

Name: Anonymous 2017-02-26 4:20

use fread and a buffer

Name: Anonymous 2017-02-26 4:26

you shouldn't be asking noob shit like this on /prog/, it's for people who already good at programming

>>4
dont help him!

Name: Steve 2017-02-26 12:34

>>3
why tf would you write in a deprecated language

Name: Anonymous 2017-02-26 12:44

>>3
wrap more than one scanf into a multiscanf function, assuming the next one will scan from the last whitespace

Name: Anonymous 2017-02-26 13:48

>>3
So not only is getc() superflous, its name is misleading about how it is used. So why is it even included in the C standard library?
C wasn't designed ahead of time like most languages. It's more like Perl and PHP. It started as a toy project and kept growing.

It was based on the untyped B language, which was inspired by the untyped BCPL language. Anything to do with types like struct, union, enum, arrays, long/short, floats, void, void* was all added later. The preprocessor and some basic libraries were also added later.

B and BCPL didn't even have C ``arrays'' just pointers. B ``arrays'' really were pointers, which were integers.

Name: Anonymous 2017-03-02 23:16

>>3
fgetc, the answer is always fgetc
or getline if you are a useless shit

Name: Steve 2017-03-02 23:19

#include ``conio.h''

Name: Anonymous 2017-03-03 7:07

>>3
fgets, and then just print one character less. sure, it would be better to have good defaults but getting around the newline isn't rocket science.

Name: Anonymous 2017-03-03 12:58

>>3
Once you get to be a decent programmer you won't be reading user input from the console except from command line arguments. Just use fgets and write a function to strip white space.

Don't change these.
Name: Email:
Entire Thread Thread List