Custom Made Safe Input Function

This is a discussion on Custom Made Safe Input Function within the C Programming forums, part of the General Programming Boards category; I am trying to make a safe input function, since it's tedious to keep writing a bunch of crap with ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    Custom Made Safe Input Function

    I am trying to make a safe input function, since it's tedious to keep writing a bunch of crap with fgets() every time you want to get some input from the user safely.

    This is what I have written so far, and I know it's far from perfect:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void gstring /*Sexy*/ (char *destination_string)
    
    {
    	char *p;
    
    	if ( (fgets(destination_string, sizeof(destination_string), stdin)) != NULL )
    	{
    		if ( ((p = strrchr(destination_string, '\n')) != NULL) )
    			*p = '\0';
    			
    		else 
    			/*ungetc('x', stdin); One idea, to always make the scanf call have a purpose*/
    			scanf("%*s");
    	}
    
    	else
    		puts("Error encountered reading string.");
    
    	return;
    
    }
    First off, I know that if nothing is left in the input buffer because the destination string and the input received are both the same size, then the scanf function is an error.

    How would I go about checking the input buffer?

    The other solution I have is to make a string in the function, of say BUFSIZ, which should be large enough to accommodate any string input, and then strncpy() the appropriate length to the destination string.

    Another idea I was thinking of, was using ungetc() to force something onto the input buffer, so that the call to scanf to clear the input buffer would always have a purpose.

    If someone could look this over and give any ideas/tips, I'd be thankful.

    Thanks.
    Last edited by Beast(); 08-20-2004 at 01:15 AM. Reason: Code Fookup

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I didn't read the whole post, because I don't need to. It will not work how you think it'll work and its all because of:
    Code:
    if ( fgets(destination_string, sizeof(destination_string), stdin) != NULL )
    Can you guess why?

    Now if you want to make your own "safe" function why not use something like fgetc() in a loop?

    Edit: [quzah mode] Maybe if you'd paid attention in class you wouldn't have made the above mistake[/quzah mode]

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by Thantos
    I didn't read the whole post, because I don't need to. It will not work how you think it'll work and its all because of:
    Code:
    if ( fgets(destination_string, sizeof(destination_string), stdin) != NULL )
    Can you guess why?

    Now if you want to make your own "safe" function why not use something like fgetc() in a loop?

    Edit: [quzah mode] Maybe if you'd paid attention in class you wouldn't have made the above mistake[/quzah mode]
    Eh, because of my lack of parenthesis? Missed that in my haste.

    I thought that calling fgetc() a bunch of times wouldn't be as efficient as one solid call to fgets().

    Thanks for Quzah mode, he should be around in a bit to let me know about that as well.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    what will
    Code:
    sizeof(destination_string)
    give you?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Lol yea, I just noticed it when I tried to compile it. Stupid mistake.

    It's the size of the pointer passed, not the string itself.

    Here is the updated function:

    Code:
    void gstring /*Sexy*/  (char *destination_string, int size)
    
    {
    	char *p;
    
    	if ( fgets(destination_string, size, stdin) != NULL )
    	{
    		if ( ((p = strrchr(destination_string, '\n')) != NULL) )
    			*p = '\0';
    			
    		else 
    			/*ungetc('x', stdin); One idea, to always make the scanf call have a purpose*/
    			scanf("%*s");
    	}
    
    	else
    		puts("Error encountered reading string.");
    
    	printf(destination_string);
    
    
    	return;
    
    }
    Last edited by Beast(); 08-20-2004 at 01:15 AM. Reason: Code Fookup

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >If someone could look this over and give any ideas/tips, I'd be thankful.

    Here is a similar attempt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Thanks man, I will mull that over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21