---
+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    scanf read string with whitespace

    Hi

    I'm writing some data aquisition code in which the folder and files that the data are stored in are user definable without changing the code. Now I've managed to do this, but I'm having a problem with 'white space' (space bar especially) in the user inputed names. I'm using scanf() to read in a user inputed string which treats white space in a manner that causes problems with my program. If it was just me that was goint to use the code then it wouldn't be a problem.
    Anyway

    pprintf("enter directory ->");
    scanf("%s", &input2);
    sprintf(dirname,"c://%s",input2);
    mkdir(dirname);
    printf("enter filename base ->");
    scanf("%s", &input);


    these are some of the pertinent parts of the code. Anywhite space in the names causes code malfunction, Is there another way of reading in a string, or is there a way of ignoring/blocking white-space entry?

    Cheers,

    Nick

  2. #2
    Newbie level 6
    Points: 1,307, Level: 8

    Join Date
    Mar 2006
    Posts
    11
    Helped
    3 / 3
    Points
    1,307
    Level
    8

    scanf c white space bar

    Hi Nick,

    Instead of using scanf to read input, try:

    fgets(buffer, buffer_length, stdin);

    This will read a line from the standard input until either a /n character is read, or buffer_length number of characters have been read. The /n character will be copied into the buffer, which you may need to strip out.

    Also, when calling the functions to access directories or files with spaces in them, you may need to enclose the string in quotes to ensure it is processed correctly by the OS.

    Hope this helps,
    Jay



    •   Alt19th February 2008, 22:06

      advertising

        
       

  3. #3
    Member level 5
    Points: 2,089, Level: 10

    Join Date
    Sep 2007
    Posts
    78
    Helped
    16 / 16
    Points
    2,089
    Level
    10

    reading whitespace in c

    The only problem being SCANF...scanf takes input well but when space bar is hit, scanf terminates taking input...the alternative for scanf is GETS...using gets you can take any input and it only terminate when enter is hit...
    so simple...replace your scanf with gets

    Best Regards



  4. #4
    Newbie level 6
    Points: 1,307, Level: 8

    Join Date
    Mar 2006
    Posts
    11
    Helped
    3 / 3
    Points
    1,307
    Level
    8

    string with white space reading

    gets() will work, but is dangerous because is does not perform bounds-checking. A user can type a string longer than your buffer and overflow it. fgets() is much safer.



  5. #5
    Member level 5
    Points: 2,089, Level: 10

    Join Date
    Sep 2007
    Posts
    78
    Helped
    16 / 16
    Points
    2,089
    Level
    10

    read input with whitespace c

    well fgets brings input from a file...
    but according to Nick the input is desired from the user/keyboard.


    Best Regards



  6. #6
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    how to read a string with white spaces in c

    I don't think I can use that to capture input from the keyboard. fgets() reads from a file right? similar to fscanf etc. or am I wrong?

    The problem I have is that the people that use the program will be making directory and file names of their choosing, all different lengths etc. I could give them instructions to not use spaces etc. but that looks a touch unprofessional. I used to be fairly good at C during my undergrad and masters years, but my memories have faded somewhat.

    There is surely a way to read a space into a character string from a keyboard input.



  7. #7
    Member level 5
    Points: 2,089, Level: 10

    Join Date
    Sep 2007
    Posts
    78
    Helped
    16 / 16
    Points
    2,089
    Level
    10

    reading string with white spaces c++ from stdin

    Correct Nick.
    exactly what i mentioned.

    Best Regards



    •   Alt19th February 2008, 22:27

      advertising

        
       

  8. #8
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    string with spaces in c -c++ -c#

    Is there some way I could encase the scanf() or whatever in a wrapper of some sort to detect if a space-bar press has occured, so I can instruct the user to input a valid name.

    I'm sure I remember doing this sort of thin years ago.

    Cheers



    •   Alt19th February 2008, 22:31

      advertising

        
       

  9. #9
    Newbie level 6
    Points: 1,307, Level: 8

    Join Date
    Mar 2006
    Posts
    11
    Helped
    3 / 3
    Points
    1,307
    Level
    8

    c - scanf does not capture whitespace

    The third argument to fgets() specifies where to read the input -- can be a file or, as I wrote above, stdin (reading console input from the keyboard).

    See http://en.wikipedia.org/wiki/Fgets


    1 members found this post helpful.

  10. #10
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    c++ string read white spaces?

    I know that gets() should never be used, ever.



  11. #11
    Member level 5
    Points: 2,089, Level: 10

    Join Date
    Sep 2007
    Posts
    78
    Helped
    16 / 16
    Points
    2,089
    Level
    10

    read a string containing whitespace c from stdin

    well by only useing scanf you can take a string input with spaces but a bit lengthy and unprofessional look...
    it will go like
    char stringa, stringb,space;
    space = ' ';
    scanf("%s%s",stringa,stringb);
    stradd(stringa,space);
    stradd(stringa,stringb);


    the last line will bring string a and b in a line.

    *the above code will only enable to take a string input with one space...
    for multiple you'll need to make it more complex.
    i would still recomment gets.

    Best Regards


    1 members found this post helpful.

  12. #12
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    c++ read string with white spaces from keyboard

    I've managed to get the fget() to work - sort of- bit as was mentioned there is a carriage return at the end of the string which is causing some problems. Is reading the string into a new string 1 char shorter the best way to strip this off, or is there a more elegant way?

    Cheers,

    Nick



  13. #13
    Member level 5
    Points: 2,089, Level: 10

    Join Date
    Sep 2007
    Posts
    78
    Helped
    16 / 16
    Points
    2,089
    Level
    10

    getting a string with whitespace from keyboard c

    in programming what i believe elegant = logic + ease
    if you don't have to show you're code to some one and you're just after getting the task done, then use what ever suits you best...

    if you have to show the code to some one then the best code is shortest code.


    Best regards



  14. #14
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    reading in white space with c

    They'll unfortunately require the source code.



  15. #15
    Member level 5
    Points: 2,089, Level: 10

    Join Date
    Sep 2007
    Posts
    78
    Helped
    16 / 16
    Points
    2,089
    Level
    10

    Re: Reading a string with white space in C

    aahhh...then you better use the shortest code but make sure its simple for ya to work with and implement what you want.

    Best Regards



  16. #16
    Newbie level 6
    Points: 1,307, Level: 8

    Join Date
    Mar 2006
    Posts
    11
    Helped
    3 / 3
    Points
    1,307
    Level
    8

    Re: Reading a string with white space in C

    Hi Nick,

    You could try this:

    Code:
    int len = strlen(string);
    if (string[len-1]=='\n')
        string[len-1]='\0'
    That should replace a trailing space in 'string' with a null-terminator.[/code]


    1 members found this post helpful.

  17. #17
    Advanced Member level 1
    Points: 4,656, Level: 16
    Old Nick's Avatar
    Join Date
    Sep 2007
    Posts
    477
    Helped
    66 / 66
    Points
    4,656
    Level
    16

    Re: Reading a string with white space in C

    cheers guys,

    I'm going to have to write a loop to search through the string for the a '/n' since I'm not going to know the length of the string they'll be inputing.

    Cheers for your help.

    Nick



  18. #18
    FvM
    FvM is offline
    Super Moderator
    Points: 171,292, Level: 98
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    27,754
    Helped
    8754 / 8754
    Points
    171,292
    Level
    98

    Re: Reading a string with white space in C

    Hello,

    it has been said, that gets() could cause string overflow, that's true, but not in a different or more dangerous way than fgets() or scanf() without length parameter, as used in the original code. So why gets() should never be used but the other functions may?

    Regards,
    Frank



  19. #19
    Newbie level 6
    Points: 1,307, Level: 8

    Join Date
    Mar 2006
    Posts
    11
    Helped
    3 / 3
    Points
    1,307
    Level
    8

    Re: Reading a string with white space in C

    fgets() and scanf() can both limit the length of the string being read:

    fgets has a buffer length argument and scanf() can specify length in the format string i.e. "%10s". gets() provides no such length checking.

    Why use unsafe functions when safe alternatives exist?

    Regards,
    Jay



  20. #20
    Advanced Member level 5
    Points: 40,208, Level: 49

    Join Date
    Apr 2002
    Location
    USA
    Posts
    3,944
    Helped
    649 / 649
    Points
    40,208
    Level
    49

    Reading a string with white space in C

    I typically use fgets() to input the string, and then sscanf() to parse the string. That helps remove white space.

    More info on scanf() from the comp.lang.c FAQ:
    http://c-faq.com/stdio/scanfprobs.html

    That FAQ is has many other good C programming tips. Here's the top page:
    http://c-faq.com/index.html



+ Post New Thread
Please login
Page 1 of 2 12 LastLast