MSDN 2010, scanf_s
-
How to spacify the width of a string in 'scanf_s' ? I feel hard, since the width of the string in the input file is not fixed varying from 50 character to 120, so how to specify the buffer size. I used 'scanf' to read input, I do not need to specify the length of the string. Please advise. Thanks
-
How to spacify the width of a string in 'scanf_s' ? I feel hard, since the width of the string in the input file is not fixed varying from 50 character to 120, so how to specify the buffer size. I used 'scanf' to read input, I do not need to specify the length of the string. Please advise. Thanks
mrby123 wrote:
How to spacify the width of a string in 'scanf_s' ?
mrby123 wrote:
I feel hard, since the width of the string in the input file is not fixed varying from 50 character to 120, so how to specify the buffer size.
If the size varies, you have to specify the maximum size, the smaller string will fit just fine in the larger strings array.
mrby123 wrote:
I used 'scanf' to read input, I do not need to specify the length of the string.
Yes, but this was a frequent cause of crashes due to buffer overflows, you don't HAVE to use scanf_s, if the previous version worked just fine for you, you can disable the warning with a pragma comment.
-
How to spacify the width of a string in 'scanf_s' ? I feel hard, since the width of the string in the input file is not fixed varying from 50 character to 120, so how to specify the buffer size. I used 'scanf' to read input, I do not need to specify the length of the string. Please advise. Thanks
mrby123 wrote:
I feel hard, since the width of the string in the input file is not fixed varying from 50 character to 120, so how to specify the buffer size.
You have to use a buffer at least equal to the maximum string length of the input file (hence at least
120
characters long) and specify the size of your buffer (that is120
) in thescanf_s
call.Veni, vidi, vici.