Thread: Reading Strings

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Reading Strings

    Ok this question probably has a simple answer but I currently can't find any answers on here or google.

    I'm relatively new to C but have experience in other programming languages.

    I'm writing a program where I need to read in a surname such as "Smith", "Jones" etc.. which is fine, by using scanf i can read these in to my program fine.

    However I now need to read in double barrell surnames along with standard ones, so the user may input, "Jones", "Smith Jones", "Smith von Jones" etc...

    Bearing in mind that I need to be able to read in any of these three cases from one input string how would I go about this? As scanf stops at whitespaces i cant do that and if I put in multiple clauses it has to be either 1, 2 or 3 words, not an option of the three.

    Any help would be appreciated.

    Cheers

  2. #2
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Use fgets() instead.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use fgets() instead of scanf(), it will copy everything you type into the variable, including the <Enter>, or '\n'. So if you don't want '\n' you will have to truncate it. The third parameter to fgets for keybord input is stdin.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    Or write your own function by using fgetc() which is reading character by character.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    ok i've written a new function using getchar()...

    Code:
        
        char name[30];
        int n=0;
        while (n <=30 && name[n] != '\n') {
              name[n] = getchar();
              n++;
              }
    however it doesnt stop when i get to the carriage return, am I using the right character for not name[n]?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    your fuction is testing the value of name[n] before there has been anything assigned to the n'th character position.
    Code:
    while( n < 30 && (name[n] = getchar()) != '\n))
       n++;

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    yup fixed that but it still waits until 30 characters (including carriage returns) have been inputted before finishing the function

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    getchar() does not return to your program until after you hit the <Enter> key. There is no standard C solution of getting the characters as they are pressed. But some compilers support non-standard solutions found in conio.h -- look for _kbhit(), getch() and getche().
    Last edited by Ancient Dragon; 11-10-2005 at 01:44 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    ok it works now by using the gets() feature

    i've set the size of the name array to be 100 chars so there should be no problems with overflow

    thoughts?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    If I have learned one thing it's that users will always find marvellous ways to mess up any program... So, don't think you're safe with a 100 char array...

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    hmm yeah but I cant seem to find any better way of doing it unless someone has a better idea?

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    well, gets() is a horrible function and should be avoided at all costs. use fgets() instead because it will not allow buffer overflows
    Code:
    char name[25];
    fgets(name,sizeof(name),stdin);

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    ah cheers thats great

    and just to check, fgets is a standard C function yeah? no C++ in there?

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dacbo
    ah cheers thats great

    and just to check, fgets is a standard C function yeah?
    yes, pure C -- no c++ there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading data from strings
    By Winston Hong in forum C Programming
    Replies: 11
    Last Post: 05-28-2008, 08:56 PM
  2. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  3. reading from a file + list of strings
    By stewie1986 in forum C Programming
    Replies: 2
    Last Post: 12-06-2007, 11:59 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM