Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MSDN 2010, scanf_s

MSDN 2010, scanf_s

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrby123
    wrote on last edited by
    #1

    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

    A CPalliniC 2 Replies Last reply
    0
    • M mrby123

      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

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      mrby123 wrote:

      How to spacify the width of a string in 'scanf_s' ?

      Like this[^]...

      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.

      1 Reply Last reply
      0
      • M mrby123

        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

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

        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 is 120) in the scanf_s call.

        Veni, vidi, vici.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups