So I can't seem to understand which would be better to use, scanf,gets, or getchar because really they do the same thing for me at least. The input is as follows /file/file-name/file_name/file.txt
and I need to capture each directory and file individually (The / is the indicator for separation). I know how to catch each individually, conceptually there's no problem here. My question is, which C function would be a better option to go with here? What are the limits of each function and what are the benefits of each?
Asked
Active
Viewed 98 times
0

John Zwinck
- 239,568
- 38
- 324
- 436

Senglish
- 115
- 1
- 3
- 9
-
1If you need to parse input ... would you prefer to parse it a character at a time, or a line at a time? There's no "right answer" - it's a choice. ALSO: *NEVER* use "gets()". *ALWAYS* prefer `fgets()`. – paulsm4 May 04 '16 at 01:54
-
is there a reason gets() is considered "bad"? @paulsm4 – Senglish May 04 '16 at 01:55
-
2@Senglish: `gets` is vulnerable to buffer overflows; if you pass it a buffer sized for 10 characters and a user enters 100, `gets` will store those extra 90 characters to the memory following the buffer, which can cause all sorts of mayhem. Because of this insecurity, it was deprecated in the 1999 standard and has been removed from the 2011 standard. Don't use it. Use `fgets` instead. – John Bode May 04 '16 at 02:03
-
Ok, thanks for the info! and the info just has to be split char by char based off of the trigger character "/" – Senglish May 04 '16 at 02:04
-
1[Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/995714) to split use `strtok` because `scanf` is used to get a fixed number of variables, not to split words into tokens – phuclv May 04 '16 at 02:07
-
Ok, thanks for the advice! Will look into using these. – Senglish May 04 '16 at 02:15
-
@JohnBode: Intriguingly, `gets()` was not deprecated in C99; it was simply removed (without warning) from the C11 standard. – Jonathan Leffler May 04 '16 at 02:22
-
Assuming the path name is alone on a line, then I'd probably use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX's [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) to read the line, and then perhaps [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) but more likely functions like [`strchr()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strchr.html) — or, for this context, perhaps [`strtok_r()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok_r.html). – Jonathan Leffler May 04 '16 at 02:26
-
Your question is the differences/benefits of *line-oriented input* verses *character-oriented input*. One additional consideration (generally, not specifically with your path) is that *line-oriented* input functions will be much more efficient due to buffering that takes place compared to a separate I/O call on every single character. – David C. Rankin May 04 '16 at 06:13
-
@JonathanLeffler: It was in the "Future Library Directions" of [n1256](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf), 7.26.9/2: "The `gets` function is obsolescent, and is deprecated." – John Bode May 04 '16 at 22:02
-
@JohnBode: Ah, yes — that was in TC3 (Technical Corrigendum 3), published in November 2007. That does count as the standard, but it was not 'the C99 standard as published in 1999'. (The deprecation was not in either TC1 or TC2.) So, there was about 4 years warning. Not that the function has gone from any libraries yet — for reasons of 'backwards compatibility'. – Jonathan Leffler May 04 '16 at 22:07