Thread: C\linux - safe reading from input

  1. #1
    Registered User pedro_velho's Avatar
    Join Date
    Jul 2003
    Posts
    7

    Unhappy C\linux - safe reading from input

    I have read that is unsafe read from stdin like I do:

    [code]
    char buffer[40];
    fscanf(stdin, "%s", buffer);
    [\code]

    Anyone know a safe way to do this? I am a noob linux user.

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Actually you can do it in two ways:
    First you can use fgets, read :
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    But if you are a alternative guy, you can use the fscanf like this:
    Code:
       char buffer[LENGTH+1]; //using LENGTH+1 to store de '\n'
       fscanf(stdin,"%LENGTHs",buffer);
    Where LENGTH is macro specifing the length of the string

  3. #3
    Registered User pedro_velho's Avatar
    Join Date
    Jul 2003
    Posts
    7

    Unhappy thanks

    That helps me a lot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM