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. scanf up to tab?

scanf up to tab?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 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.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hi! Is it possible using scanf to read a string from STDIN up to a tab character? I.e. the tab character must be regarded as %s terminator somehow. What must the format string look like? Thanks in advance and best regards, Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    B 1 Reply Last reply
    0
    • D Dominik Reichl

      Hi! Is it possible using scanf to read a string from STDIN up to a tab character? I.e. the tab character must be regarded as %s terminator somehow. What must the format string look like? Thanks in advance and best regards, Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

      B Offline
      B Offline
      Bhaskar
      wrote on last edited by
      #2

      Use scanset. char s[80]; scanf(" %[^\t]", s);

      H 1 Reply Last reply
      0
      • B Bhaskar

        Use scanset. char s[80]; scanf(" %[^\t]", s);

        H Offline
        H Offline
        Harold Bamford
        wrote on last edited by
        #3

        scanf() is a dangerous function because it is easy to have a buffer overflow. Try using fgets() and sscanf() instead. Both use buffers of specified size.

        B 1 Reply Last reply
        0
        • H Harold Bamford

          scanf() is a dangerous function because it is easy to have a buffer overflow. Try using fgets() and sscanf() instead. Both use buffers of specified size.

          B Offline
          B Offline
          Bhaskar
          wrote on last edited by
          #4

          You can limit the no of characters to extract with scanf. char s[BUFFERSIZE+1]; scanf(" %*[^\t]", BUFFERSIZE, s);

          H 1 Reply Last reply
          0
          • B Bhaskar

            You can limit the no of characters to extract with scanf. char s[BUFFERSIZE+1]; scanf(" %*[^\t]", BUFFERSIZE, s);

            H Offline
            H Offline
            Harold Bamford
            wrote on last edited by
            #5

            Bhaskar wrote: You can limit the no of characters to extract with scanf. char s[BUFFERSIZE+1]; scanf(" %*[^\t]", BUFFERSIZE, s); It is true that you can specify the maximum width of characters assigned to a string. However, this is hard to read, and prone to error. Format strings are inherently hard to use (but extremely powerful). I just re-read the help section in MSVC++, "Format Specification Fields: scanf and wscanf Functions" and I don't believe your example is valid. For one thing, it is missing a type specifier ('s'). This is such an easy mistake (which I have done myself) that it only shows the danger in using scanf(). And even if this turns out to be a valid format string, it is almost unreadable.

            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