Bad File Descriptor Error on strcat/strcpy

Discussion in 'C Programming' started by lynology, Aug 18, 2004.

  1. lynology

    lynology Guest

    I need help trying to figure why this piece of code gives me a "Bad
    File descriptor error" everytime I try to run it and invoke fflush.
    This piece of code simple outputs a char string to the output stream.
    What ends up happening instead is that when outputting the first
    character string to the channel CG_cdukeypad_CHA.Scrpad, its gives the
    bad file descriptor error, causing the char string not to output. When
    a second char string is received by the pointer and copied to the
    channel, the first character string appears on the output stream
    without the error message. When the third char string is received, the
    second char string appears on the output stream w/o the error msg, so
    on and so forth.


    Quote:
    CG_cdukeypad_CHA.Scrpad is a channel to the output stream.
    cdukeypad->Scrpad points to the value that I want to pass to the
    output stream.

    Code:
    if (strlen(cdukeypad->Scrpad) > 0){ 
      printf("Character received is %s.\n", cdukeypad->Scrpad); 
        if (strlen(CG_cdukeypad_CHA.Scrpad) < 25){ 
            strcat(CG_cdukeypad_CHA.Scrpad, cdukeypad->Scrpad); 
        } 
        if (fflush(NULL) == EOF){ 
          VP_INFO(("Error writing text to Scratchpad.\n")); 
          VP_INFO(("Error is %s\n", strerror(errno))); 
        } 
      } 
    
     
    lynology, Aug 18, 2004
    #1
    1. Advertising

  2. lynology

    -berlin.de Guest

    lynology <> wrote:
    > I need help trying to figure why this piece of code gives me a "Bad
    > File descriptor error" everytime I try to run it and invoke fflush.


    > if (strlen(cdukeypad->Scrpad) > 0){
    > printf("Character received is %s.\n", cdukeypad->Scrpad);
    > if (strlen(CG_cdukeypad_CHA.Scrpad) < 25){
    > strcat(CG_cdukeypad_CHA.Scrpad, cdukeypad->Scrpad);
    > }
    > if (fflush(NULL) == EOF){


    NULL isn't a valid file pointer. What that's supposed to do?
    Perhaps you mean (since you just have used printf() which
    prints to stdout)

    if ( fflush( stdout ) == EOF )

    But be aware that even fflush() does only flush the user space
    buffers provided by the C library. More buffering could happen
    at lower levels.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 18, 2004
    #2
    1. Advertising

  3. -berlin.de scribbled the following:
    > lynology <> wrote:
    >> I need help trying to figure why this piece of code gives me a "Bad
    >> File descriptor error" everytime I try to run it and invoke fflush.


    >> if (strlen(cdukeypad->Scrpad) > 0){
    >> printf("Character received is %s.\n", cdukeypad->Scrpad);
    >> if (strlen(CG_cdukeypad_CHA.Scrpad) < 25){
    >> strcat(CG_cdukeypad_CHA.Scrpad, cdukeypad->Scrpad);
    >> }
    >> if (fflush(NULL) == EOF){


    > NULL isn't a valid file pointer. What that's supposed to do?
    > Perhaps you mean (since you just have used printf() which
    > prints to stdout)


    > if ( fflush( stdout ) == EOF )


    > But be aware that even fflush() does only flush the user space
    > buffers provided by the C library. More buffering could happen
    > at lower levels.


    Haven't you heard about fflush(NULL)? It flushes all currently open
    output streams. NULL isn't normally a valid parameter for the file
    IO functions but fflush is a special case.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Make money fast! Don't feed it!"
    - Anon
     
    Joona I Palaste, Aug 18, 2004
    #3
  4. lynology

    -berlin.de Guest

    Joona I Palaste <> wrote:
    > -berlin.de scribbled the following:
    >> lynology <> wrote:
    >>> I need help trying to figure why this piece of code gives me a "Bad
    >>> File descriptor error" everytime I try to run it and invoke fflush.


    >>> if (strlen(cdukeypad->Scrpad) > 0){
    >>> printf("Character received is %s.\n", cdukeypad->Scrpad);
    >>> if (strlen(CG_cdukeypad_CHA.Scrpad) < 25){
    >>> strcat(CG_cdukeypad_CHA.Scrpad, cdukeypad->Scrpad);
    >>> }
    >>> if (fflush(NULL) == EOF){


    >> NULL isn't a valid file pointer. What that's supposed to do?
    >> Perhaps you mean (since you just have used printf() which
    >> prints to stdout)


    >> if ( fflush( stdout ) == EOF )


    >> But be aware that even fflush() does only flush the user space
    >> buffers provided by the C library. More buffering could happen
    >> at lower levels.


    > Haven't you heard about fflush(NULL)? It flushes all currently open
    > output streams. NULL isn't normally a valid parameter for the file
    > IO functions but fflush is a special case.


    Sorry, no I haven't seen that. You never stop learning. Thanks for
    pointing it out!
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 18, 2004
    #4
  5. lynology

    Chris Torek Guest

    In article <news:>
    lynology <> wrote:
    >I need help trying to figure why this piece of code gives me a "Bad
    >File descriptor error" everytime I try to run it and invoke fflush.

    [snippage]
    >if (strlen(cdukeypad->Scrpad) > 0){
    > printf("Character received is %s.\n", cdukeypad->Scrpad);
    > if (strlen(CG_cdukeypad_CHA.Scrpad) < 25){
    > strcat(CG_cdukeypad_CHA.Scrpad, cdukeypad->Scrpad);
    > }
    > if (fflush(NULL) == EOF){
    > VP_INFO(("Error writing text to Scratchpad.\n"));
    > VP_INFO(("Error is %s\n", strerror(errno)));
    > }
    > }
    >[/CODE]


    (a) What is VP_INFO?

    (b) "errno" is, in effect, a "global variable". Global variables
    have some very bad properties.

    (c) Let me suppose, just for argument, that VP_INFO turns into an
    fprintf to stderr or similar. Let me suppose further that
    fflush(NULL) (which f{flush()es all open output streams) is
    in fact returning EOF for some reason, and -- despite the nasty
    nature of a single global "errno" -- has managed to record the
    reason for the (possibly dozens of) failure(s) in "errno".
    Alas, the very first fprintf() to stderr invokes some OS
    code that tests to see if stderr is connected to an interactive
    device. This code happens to clobber errno, setting it to
    EBADF ("Bad file descriptor"). Then:

    - fflush(NULL) returns EOF and sets errno
    - the first VP_INFO(...) destroys that errno, replacing
    it with a new value
    - the second VP_INFO(...) invokes strerrno on the bogus
    errno.

    (d) "Global variables" (including errno) have some very bad
    properties. I realize this is actually just one flaw, but
    it is so huge I decided to mention it twice. :) [I have
    a keyring that says "my other spaceship is the Red Dwarf"]

    Anyway, because the errno mechanism is such a fragile one, you
    must make sure you capture the value immediately after a failure:

    if (fflush(NULL) == EOF) {
    int e = errno;
    ... /* things that may clobber errno, but we saved it */
    ... strerror(e) ...
    }

    or:

    if (fflush(NULL) == EOF) {
    const char *s = strerror(errno);
    ... /* more things that may clobber errno */
    ... s ...
    }

    Since strerror() is also allowed to use static data, the first form
    is generally a bit safer.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Aug 18, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JC

    strcpy and strcat problem

    JC, Sep 26, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,925
    Robert B. Clark
    Sep 29, 2003
  2. Jarmo
    Replies:
    44
    Views:
    1,801
    Irrwahn Grausewitz
    Nov 26, 2003
  3. Pascal Damian

    Difference between strcpy() and strcat()?

    Pascal Damian, Mar 5, 2004, in forum: C Programming
    Replies:
    9
    Views:
    13,085
    Pascal Damian
    Mar 6, 2004
  4. Matt
    Replies:
    82
    Views:
    1,828
    Douglas A. Gwyn
    Sep 29, 2004
  5. strcpy and strcat's return type

    , Jul 26, 2005, in forum: C Programming
    Replies:
    14
    Views:
    813
    Kenneth Brody
    Aug 22, 2005
Loading...

Share This Page