0

I have 2 fgets into my code and both of them aren't wait for input...
This is an example of the first fgets...

printf("Insert path: ");
if(fgets(dirpath, BUFFGETS, stdin) == NULL){
    perror("fgets dir path");
    close(sockd);
}

and, as i've written before, also the next fgets is not waiting for my input :(
Before the first fgets i have 2 scanf("%ms", &string); (if this could be the trouble).

polslinux
  • 1,739
  • 9
  • 34
  • 73
  • 1
    Any reason you can't just use `scanf` again? –  Jul 09 '12 at 17:59
  • What is the `'m'` doing in that `scanf()`? Show us the real code (including the `scanf()` calls). – Michael Burr Jul 09 '12 at 18:03
  • 5
    Also, look at http://stackoverflow.com/a/6083941/12711 - it probably is related to your problem. Remember that the "%s" format in `scanf()` will stop reading at the first whitespace, leaving the whitespace in the stream. A newline is a whitespace character. – Michael Burr Jul 09 '12 at 18:06
  • See this (http://stackoverflow.com/questions/2329909/dynamic-string-input-using-scanfas) for %ms –  Jul 09 '12 at 18:10
  • @Arkadiy: do you have a link to docs for the `m` flag character? I see the `a` flag (which is new to me) discussed in the GCC C lib docs, but the not the `m` flag. The SO question you link to simply says you should use `m` instead of `a` if using a new enough glibc, but there's no explanation of why or what's different. – Michael Burr Jul 09 '12 at 18:20
  • Some discussion of the `m` flag for formatted input in glibc: http://gcc.gnu.org/ml/gcc-patches/2007-09/msg01342.html - I wonder why this hasn't made it into the GCC docs? – Michael Burr Jul 09 '12 at 18:29
  • Here is the GNU doc on %as: http://www.gnu.org/software/libc/manual/html_node/Dynamic-String-Input.html –  Jul 09 '12 at 18:35
  • @MichaelBurr: It's not in the GCC docs because it's not part of gcc; it's part of glibc. – Keith Thompson Jul 09 '12 at 19:25
  • @KeithThompson: fair enough, please forgive my imprecise wording. But it's not in the GNU C Library docs either (or more specifically: http://www.gnu.org/software/libc/manual/html_node/index.html). – Michael Burr Jul 09 '12 at 19:30
  • Hassan: i cannot use scanf because it doesn't accept whitespace MichaelBurr: 1) "http://gcc.gnu.org/ml/gcc-patches/2007-09/msg01342.html" 2) hey you've solved my problem :D love you! – polslinux Jul 09 '12 at 20:33

1 Answers1

1

i think scanf does not read in the linebreak. You can try to read it in first with an additional fgets after scanf().

DasMoeh
  • 51
  • 2
  • @Arkadiy: Don't use `"%s"` with `scanf`; it's prone to buffer overflows. It doesn't specify a maximum input length, so arbitrary input will overflow any possible buffer. – Keith Thompson Jul 09 '12 at 19:26
  • The important part is \n. %s really should be %ms as in the original code. Sorry about the confusion. –  Jul 11 '12 at 23:08