Thread: need help with char array and fgets

  1. #16
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Elysia View Post
    But is it necessary?
    I/O are buffered for a reason.
    The stdout buffer is guarantee to display to screen only after it encounters a '\n'.
    Because the lack of a '\n' in the printf() function I will say that is not only most appropriated to call fflush() right afterward, but also good practice to do so.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't agree it's a good practice. If printf isn't guaranteed to display the contents because of the lack of newline, then it's flawed and should be avoided with other functions instead.
    There's fputs, fprintf too, though I don't know how they perform.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Elysia View Post
    That strikes me odd. Never had such a problem, not anyone else who posted their samples in C here, from what I can tell (unless they didn't run their samples?).
    Isn't that the same philosophy that new programmers use respecting practices concerning bad usage of the language?
    It works for me. So then is it good.
    Quote Originally Posted by Elysia View Post
    Anyway then, remove that fflush and test if it works. IF it doesn't, THEN add the fflush.
    Flushing when not needed is not a good thing.
    Again. Ugly philosophy. Just because it works for a particular compiler doesn't mean is appropriated to do or not do. Learning correct C language is beyond any specific compiler behaviour.
    Don't encourage the opposite of what you are trying to preach.

  4. #19
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well fputs and fprintf are going to have the same "flaw". This has nothing to do with the functions themselves; it is simply the way buffered streams work.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Aia View Post
    Isn't that the same philosophy that new programmers use respecting practices concerning bad usage of the language?
    It works for me. So then is it good.
    Entirely different things! Defines/Macros != functions. Incorrect use of functions can cause runtime problems, crashes, security issues, etc. But macros do NOT.

    Again. Ugly philosophy. Just because it works for a particular compiler doesn't mean is appropriated to do or not do. Learning correct C language is beyond any specific compiler behaviour.
    Don't encourage the opposite of what you are trying to preach.
    I try to encourage avoiding flushing, simply because buffering is a good thing.
    Then if it will sidestep them problem, I recommend putting a \n to ensure flushing does not need to be done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Calling fflush() on stdout before a printf() prompt that contains no '\n' is good practice.

    C++ IO causes the output buffer to flush when input functions are invoked. In addition, everytime std::endl is used, it causes an automatic flush of the output buffer, so "works for me" from a C++ programmer is useless in this regard.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So basically the entire I/O is flawed instead, if you ask me. Oh well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're limited by the human user. He has to see what is on the screen in order to respond, so you have to flush the buffer. I'm not sure why you would think that is a wasted action. Sure, if the user could wait until the buffer is full, that would be great, but that's not realistic in many programs that require user input.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So yes, the output text should be seen on the screen and the input should be input directly after returning (with whatever means). So let's end this boring discussion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most C runtime libraries will flush stdout before reading stdin, but I know for a fact that in the old days [e.g. 1980's] fflush was necessary if you wanted input directly after a prompt, because printf would not flush the buffer until a newline appeared, and a call to fgets() or similar would not flush the buffer either.

    So if the OP uses for example Turbo C, then it would be necessary. For a compiler produced in the last few years, it's most likely not necessary.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I believe some people have posted before about this. I thought I saw a Linux example where it was needed, but I could be mistaken.

    I would still recommend manually fflush(stdout)ing to guarentee the flush actually happens for prompts. Portable code and all that stuff. I'd rather not rely on these types of compiler extensions for something so trivial that barely demands more typing, but is important to the user.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find '&' character in char array?
    By tidemann in forum C Programming
    Replies: 7
    Last Post: 10-19-2006, 05:04 AM
  2. How do you strip newline from char array?
    By pityocamptes in forum C Programming
    Replies: 7
    Last Post: 04-21-2006, 03:03 AM
  3. char array size question, please help!
    By Ash1981 in forum C Programming
    Replies: 4
    Last Post: 01-29-2006, 02:30 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM