C\linux - safe reading from input

This is a discussion on C\linux - safe reading from input within the C++ Programming forums, part of the General Programming Boards category; I have read that is unsafe read from stdin like I do: [code] char buffer[40]; fscanf(stdin, "%s", buffer); [\code] Anyone ...

  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, 01: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, 08: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, 10:28 PM

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