Thread: fputs

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    fputs

    I understand that fgets is the best for user input, comparing with scanf and gets. What is going on with the output? Is puts safe to use, and how much data this function can handle? If you use fputs the output is redirected only to the file, or you can redirect it to somewhere else? This is a lot of questions I apologize.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    fputs() is certainly safe to use and is actually faster than fprintf(). There's no limit to how much data it can handle. The second argument to fputs() is the stream you want to write to, so if you do fputs("This is text.", stdout); it will send it to the screen, which is the same thing as just calling puts(). Anyway, fputs() can write to any stream.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    I understand that fgets is the best for user input, comparing with scanf and gets.
    Well it depends what input you are reading in..
    if your reading a line of input that has spaces and stuff then fgets is pretty good...
    but if your reading in one string, then i prefer scanf because it doesnt keep the trailing '\n' wehre fgets does...

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by paperbox005
    Well it depends what input you are reading in..
    if your reading a line of input that has spaces and stuff then fgets is pretty good...
    but if your reading in one string, then i prefer scanf because it doesnt keep the trailing '\n' wehre fgets does...
    It may not pull the \n off the input buffer, but a lot of times you have to remove it from the input buffer after the scanf() call.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    yeah it might stick inthe input buffer, but it aint in the string.

  6. #6
    Quote Originally Posted by itsme86
    The second argument to fputs() is the stream you want to write to, so if you do fputs("This is text.", stdout); it will send it to the screen, which is the same thing as just calling puts().
    There is a diffference. puts() adds a trailing '\n', while fputs() does not.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    As far as the program is concerned, there is no problem with using puts() or fputs()

    The only real problem is filling up the file system if you write a lot of data, but that is true no matter what file writing functions you call.

    If you try and write binary data to stdout, you could probably lock up your terminal session if it understands ANSI escape sequences. That doesn't affect your program, only your ability to control it.

    > and how much data this function can handle?
    Both stop at the first \0 in the buffer you supply.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets, fputs, and malloc.
    By eXeCuTeR in forum C Programming
    Replies: 41
    Last Post: 11-30-2007, 07:09 AM
  2. fputs only write first part of string
    By jamie85 in forum C Programming
    Replies: 4
    Last Post: 11-17-2005, 08:18 AM
  3. Question about fputs
    By nizbit in forum C Programming
    Replies: 6
    Last Post: 03-03-2005, 07:19 PM
  4. fputs and fprintf
    By fosca in forum Linux Programming
    Replies: 2
    Last Post: 12-17-2001, 07:15 AM
  5. fputs and sequential file
    By sballew in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 04:48 PM