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. Read a string with spaces

Read a string with spaces

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 4 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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I want to read a user input with spaces, say "Hello World". With scanf how can i do that. Because all the time after the space, I cannot read the input.

    scanf("%[^\n]", name);
    scanf("%s",&name);
    fscanf(stdin, "%*s%99[^\n]", name);

    None of the above solve my issue.

    I appreciate your help all the time... CodingLover :)

    G _ 2 Replies Last reply
    0
    • C CodingLover

      Hi all, I want to read a user input with spaces, say "Hello World". With scanf how can i do that. Because all the time after the space, I cannot read the input.

      scanf("%[^\n]", name);
      scanf("%s",&name);
      fscanf(stdin, "%*s%99[^\n]", name);

      None of the above solve my issue.

      I appreciate your help all the time... CodingLover :)

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      You can't do it with scanf - use gets[^] instead. Edit before someone else says it: of course gets isn't safe to use since you can't specify the buffer size, so use fgets[^] instead.

      modified on Wednesday, September 7, 2011 2:51 PM

      C 1 Reply Last reply
      0
      • C CodingLover

        Hi all, I want to read a user input with spaces, say "Hello World". With scanf how can i do that. Because all the time after the space, I cannot read the input.

        scanf("%[^\n]", name);
        scanf("%s",&name);
        fscanf(stdin, "%*s%99[^\n]", name);

        None of the above solve my issue.

        I appreciate your help all the time... CodingLover :)

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        scanf("%[^\n]", name); works. If it does not, then check how name is declared. It should be something like char name[1024];

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        C 1 Reply Last reply
        0
        • _ _Superman_

          scanf("%[^\n]", name); works. If it does not, then check how name is declared. It should be something like char name[1024];

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          C Offline
          C Offline
          CodingLover
          wrote on last edited by
          #4

          I did exactly the same, but didn't work mate.

          I appreciate your help all the time... CodingLover :)

          _ 1 Reply Last reply
          0
          • G Graham Breach

            You can't do it with scanf - use gets[^] instead. Edit before someone else says it: of course gets isn't safe to use since you can't specify the buffer size, so use fgets[^] instead.

            modified on Wednesday, September 7, 2011 2:51 PM

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            Simply I tried the following, but it didn't work either

            char test[1024];
            printf("\nTest ");
            gets(test);

            I appreciate your help all the time... CodingLover :)

            1 Reply Last reply
            0
            • C CodingLover

              I did exactly the same, but didn't work mate.

              I appreciate your help all the time... CodingLover :)

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              Run this and tell me what you get -

              char name[1024];
              scanf("%[^\n]", name);
              printf("[%s]", name);

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++)

              Polymorphism in C

              C 1 Reply Last reply
              0
              • _ _Superman_

                Run this and tell me what you get -

                char name[1024];
                scanf("%[^\n]", name);
                printf("[%s]", name);

                «_Superman_»  _I love work. It gives me something to do between weekends.

                _Microsoft MVP (Visual C++)

                Polymorphism in C

                C Offline
                C Offline
                CodingLover
                wrote on last edited by
                #7

                It simply print some garbage characters. Even in debug it'll not looking for the user input.

                I appreciate your help all the time... CodingLover :)

                _ 1 Reply Last reply
                0
                • C CodingLover

                  It simply print some garbage characters. Even in debug it'll not looking for the user input.

                  I appreciate your help all the time... CodingLover :)

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #8

                  That's strange. It works as expected for me. Are you running it from a console application? If you can give me more details like the environment that you're using and the type of program you're using, I could try to recreate the issue at my end.

                  «_Superman_»  _I love work. It gives me something to do between weekends.

                  _Microsoft MVP (Visual C++)

                  Polymorphism in C

                  C 1 Reply Last reply
                  0
                  • _ _Superman_

                    That's strange. It works as expected for me. Are you running it from a console application? If you can give me more details like the environment that you're using and the type of program you're using, I could try to recreate the issue at my end.

                    «_Superman_»  _I love work. It gives me something to do between weekends.

                    _Microsoft MVP (Visual C++)

                    Polymorphism in C

                    C Offline
                    C Offline
                    CodingLover
                    wrote on last edited by
                    #9

                    Sorry for the late reply! Yes it was a console application. I did this on VS 2008. However when I use cin instead of scanf, works fine.

                    I appreciate your help all the time... CodingLover :)

                    A 1 Reply Last reply
                    0
                    • C CodingLover

                      Sorry for the late reply! Yes it was a console application. I did this on VS 2008. However when I use cin instead of scanf, works fine.

                      I appreciate your help all the time... CodingLover :)

                      A Offline
                      A Offline
                      abhishek biradar
                      wrote on last edited by
                      #10

                      Same code worked fine for me am also using vs2008

                      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