0

I've been scouring the globe as to why scanf() is not capturing spaces, and the usual solutions such as "%[^\n]s" or "%100s" (setting string large enough to capture input) have not been working.

The overall goal of my program is to prompt the user to enter a string, and then that string be stored in a .txt file. I don't believe my problem is arising from fprintf(), rather from the initial console scan.

Yes, I am a beginner and do not understand the nuances of C. Apologies for seemingly simple syntactic errors. Here's what I got.

#include <stdio.h>  
  
int main()
{
    errno_t inputfile;
    FILE *stream;

    inputfile = fopen_s(&stream, "inputfile.txt", "w");
    printf("%s", "Enter a string.  Press ENTER to exit\n");

    char c[100];
    scanf("%[^\n]s", c);
    printf("%100s", c); //using printf() to check if scanf() worked.  
                        //Messing around with specifier to see if problem 
                        //is in printf(), which doesn't seem to be the case.

    fprintf(stream, "%s", c);
    fprintf(stream, "%s", "ENTER is a correct ending");
    fclose(stream);

    return 0;
}

Any help is appreciated.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
CPKEN
  • 11
  • 3
  • *I am a beginner and do not understand the nuances of c++.* The code above is the clean C. – 273K Aug 26 '21 at 20:09
  • There's not anything obviously wrong with your code. I tried it and it worked okay. The `s` in `"%[^\n]s"` is wrong, but that's not causing any problem. The `100` in `printf("%100s"` is strange, and might be causing the output to show up in a strange way on your screen. Try changing the printf format string to `"%s\n"`. – Steve Summit Aug 26 '21 at 20:41
  • 1
    Instead of `scanf("%[^\n]s", c);`, use `scanf("%99[^\n]", c);` (omit the 's' and add a max field width.) – William Pursell Aug 26 '21 at 20:49
  • Since no one bother to link any docs, [read this](https://en.cppreference.com/w/c/io/fscanf). Specifically pay attention to the set-notation `[...]` vs. `%s` string format specifiers. They're not conjunctive. Use one or the other. This is an *incredibly* common mistake by newcomers to C, and I continue to wonder what online resources/tutorials, etc. show this erroneous form. – WhozCraig Aug 26 '21 at 21:06
  • 1
    The other thing is that `%[...]` falls into the category of "advanced `scanf` usage", but "advanced `scanf` usage" is sort of like "advanced tricycle riding". `scanf` is a toy function, useful only for the most basic, introductory programs. By the time you're ready to do something more advanced (like, "read a string possibly containing spaces"), it's time to graduate beyond using `scanf` and learn how to [use something better](https://stackoverflow.com/questions/58403537). – Steve Summit Aug 26 '21 at 21:21
  • But if you must use `"%[...]"`, besides leaving off the incorrect `s` after it, you will want to get into the habit of always putting an extra space before it: `" %[...]"`. You don't need it in the program you asked about here, where `"%[...]"` is in the very first (and only) `scanf` call, but if you ever try to use `"%[...]"` *after* scanfing anything else, you'll need the space, otherwise you'll be back here asking why it doesn't work. – Steve Summit Aug 26 '21 at 21:22

1 Answers1

2

scanf("%[^\n]s", c); is not correct, remove the s at the end of the specifier, you should also use a width limiter to avoid buffer overflow.

It should be:

scanf("%99[^\n]", c); // leave 1 character for the null byte

Notwithstanding, the behavior you describe is unexpected as you can see here:

https://godbolt.org/z/enq93MaG5

Note that I turned your code in a minimal reproducible example, i.e., I removed the unnecessary code, that's what you should do from now on.

Also note that printf("%100s", c); will have the, perhaps undesired, effect of adjusting your string to the right printing the remaining characters as blank spaces to the left, e.g. a string with 10 characters will print 90 blank spaces and then the string.

anastaciu
  • 23,467
  • 7
  • 28
  • 53