Thread: i/o error checking question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    i/o error checking question

    I want to know how I can check if a message was too long and truncated or what when I read in a string. If I do:

    fgets(iname,MAX_NLEN,stdin);

    or something similar it just truncates my string down to MAX_NLEN which is fine I just need to know if it does that or not. You would think it would return an error code or something stating that it did something like that.

    Basically, I have a loop that wants to keep reading in a name and password until they input something in a valid format. This name will be stored in a file... but if the name is too long it gets truncated. I don't want to just truncate their input and save that... they wouldn't have any idea what was put in the file when they go to use that name. It's a function to create a new user.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's simple... call fgets() again... fgets does not truncate stuff... it just stops reading when you hit the maximum number of characters. A second call to fgets() should fetch the rest of the data...

    If you're getting user input from keyboard... deal with the over-length problems there...
    Code:
    #define MAX 50;
    char input[max +10 ];
    scanf("%s", input);
    If (strlen(input > max)
      puts("Too long... ");
    2 things...

    1) Users are evil creatures who will torture your software beyond endurance
    and
    2) If it's more important than naval lint... you need to get confirmation.

    Code:
    Your username will be :  What you entered
    Your password will be :  graslaneiadeciek
    
    Is this acceptable (y/n) ?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Well, I have a bizarre error. It works now except that it prints out "Input:" twice. I tried putting a previous printf() call before that one to see if it would print "Input"+"mynewprintfmsg"+"Input" but it didn't... only when it calls THAT exact printf does it print out the extra "Input"

    Code:
    	do{
    		printf("Input:");
    		res = fgets(input,MAX_ILEN,stdin);
    		fflush(stdin);
    		formatstr(input);
    
    		if(strcmp(input,"cuser")==0){
    
    			while(iname[0] == 0){
    				printf("Enter Name:");
    				scanf("%s",iname);
    				
    				if(strlen(iname)>MAX_NLEN){
    					printf("Error try again (max length = 30).\n");
    					memset(&iname,0,MAX_NLEN);
    				}
    
    			}
    			while(ipwd[0] == 0){
    				printf("Enter Password:");
    				scanf("%s",ipwd);
    
    				if(strlen(ipwd)>MAX_PLEN){
    					printf("Error try again (max length = 30).\n");
    					memset(&ipwd,0,MAX_PLEN);
    				}
    			}
    			//cuser(iname,ipwd);
    			memset(&iname,0,MAX_NLEN);
    			memset(&ipwd,0,MAX_PLEN);
    			memset(&input,0,MAX_ILEN);
    		}
    	}while(res != NULL && ((strcmp(input,"quit")!=0)));

    I have a feeling it could do with the memset() calls I'm doing but I'm not sure. memset() them all to 0 should be ok right?

    I tried changing them to for loops and setting them that way but it didn't fix the problem so I don't know

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    About memeset()... you probably don't need any of the memset() calls... you can just do iname[0] = 0;

    Also you should not use fflush() on stdin... it doesn't do what you think it does.

    For "Input"+"mynewprintfmsg"+"Input" ... you can't do that in C.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    I'm not trying to concatenate strings via the + operator... I was just describing the expected outputed from some of the debugging I was doing. Basically it works normally except for that exact printf("Input:") call. I figured an extra "Input:" might be hanging around somewhere for some reason so by putting a printf() elsewhere I should've been able to get a message similar to 'mymsgInput;Input:" or "Input:mymsgInput:"

    But, it's that exact printf("Input:") call that printed twice for some weird reason. So, I changed it to scanf like I did to the others. It fixed that problem but now I have a new error.

    If I try putting in a msg that's too long then quitting the program via the "quit" command it will segment fault. If I just quit or don't put in a msg too long into my input then it works fine.
    Code:
    	do{
    		while(!valid){
    			printf("Input:");
    			scanf("%s",input);
    			
    			if(strlen(input)>=MAX_ILEN){
    				printf("Error: command is too long (max length = %d)\n",MAX_ILEN);
    				memset(&input,0,MAX_ILEN);
    			}
    			else{
    				valid = true;
    				printf("does it segment fault here?");
    			}
    		}
    
    		if(strcmp(input,"cuser")==0){
    
    
    			while(iname[0] == 0){
    				printf("Enter Name:");
    				scanf("%s",iname);
    				
    				if(strlen(iname)>=MAX_NLEN){
    					printf("Error try again (max length = %d).\n",MAX_NLEN);
    					iname[0]=0;
    				}
    
    			}
    
    			while(ipwd[0] == 0){
    				printf("Enter Password:");
    				scanf("%s",ipwd);
    
    				if(strlen(ipwd)>=MAX_PLEN){
    					printf("Error try again (max length = %d).\n",MAX_PLEN);
    					ipwd[0]=0;
    				}
    
    			}
    
    			//cuser(iname,ipwd);
    
    			input[0]=0;
    			ipwd[0]=0;
    			iname[0]=0;
    
    		}
    
    		else if(strcmp(input,"quit")==0){
    			end=true;
    		}
    
    		valid = false;
    	}while(!end);

    Edit, I should note that it doesn't print out the message "does it segment fault here?" if it does segment fault... so it's inside that part somewhere...
    Last edited by Homunculus; 02-14-2011 at 07:30 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try commenting out the memset() in that bit of code and see what happens...

    Something to think about... Instead of all this convoluted code you have, why not write a GetStrLimited() function that will fetch a string of limited length for you. In the function include code to re-get in case of error. When you need something from the keyboard, call your function and trust it's returned value. That way you have all your input and input error trapping in a single routine that will be far easier to debug than what you have now.

    Code:
    // put length limited string in buffer
    void GetStrLimited(char* Buffer, int limit)
      {char inbuff[256];   // longer than longest input
        int    ok = 0;          // ok to copy flag 
        while (!ok)
           { scanf("%256s", inbuff);
              if (strlen(inbuff) <= limit)
                ok = 1;
              else
                printf("Entry too long, please re-enter : ");  }
         strcpy(Buffer,inbuff); }
    Then in various places in your code you can call it as GetStrLimited(namebuff,32); and it will place a successful entry into your specified buffer for you or keep re-trying until the operator gets it right.
    Last edited by CommonTater; 02-14-2011 at 08:14 AM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    I did... then changed it to input[0]=0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 05:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  4. File I/O Question
    By 182 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2005, 03:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM