Thread: cheking the validity of data from stdin

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    40

    cheking the validity of data from stdin

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_READ 2
    
    int main( )
    {
        char * secret = "abecedarium consisting This is a string literal!";
        char command[MAX_READ], string[MAX_READ];
        
        sscanf(secret,"%s%s",command, string);    
        printf("%s %s\n",command, string);
        
        return 0;
    }
    compiling options: gcc -g -Wall -ansi -pedantic testes.c -o tt

    i am trying to test what happens in the sscanf converted types (command and string) if they are smaller, i got surprised that the printf worked, so i dont know why.

    this code is just a test for what i want to do, what i really want to do is for example: i have this string from the stdin which must have the following format:
    connect name.surname
    so i need to check the cases when it is not the input that i want, nammely if the name.surname is for example bigger than 30 chars. and also if it has the abecedary characters from the ascii,

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by tindala View Post
    i am trying to test what happens in the sscanf converted types (command and string) if they are smaller, i got surprised that the printf worked, so i dont know why.
    You are probably running into undefined behaviour. The space you set aside for command and string in the above example is not large enough to hold the strings that you are giving it. When you use %s in scanf, you have to make sure there is enough room in the destination

    Quote Originally Posted by tindala View Post
    what i really want to do is for example: i have this string from the stdin which must have the following format:
    connect name.surname
    In that case make use of %[...] in the scanf format and give the allowed characters. Then you must make sure each token corresponds to your specs (length and so on). Here is an example:
    Code:
    int main()
    {
        char secret[LINE_MAX] = "command john.smith";
        char token[3][TOKEN_MAX];
        sscanf(secret, "%[a-zA-Z] %[a-zA-Z].%[a-zA-Z]", token[0], token[1], token[2]);
        for (int i=0; i < 3; i++) {
            printf("token%d: %s\n", i, token[i]);
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c99tutorial View Post
    You are probably running into undefined behaviour.
    That's a certainty. One of the ostensible joys of code with undefined behaviour is that it can seem to work in a well defined manner. That doesn't make it correct. It simply means the error has not been detected.

    Unfortunately, programmers are often inclined to believe it is correct on the basis of it running, correctly, once ....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check stdin for piped data without blocking
    By HowardL in forum C# Programming
    Replies: 0
    Last Post: 10-19-2009, 09:16 AM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. Determining if stdin stream holds data
    By Stack Overflow in forum C Programming
    Replies: 3
    Last Post: 01-28-2005, 02:52 PM
  4. What data type are stdout and stdin?
    By KwikDrawMcGraw in forum C Programming
    Replies: 1
    Last Post: 07-16-2002, 01:34 PM

Tags for this Thread