Bad File Descriptor Error on strcat/strcpy

L

lynology

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))); 
    } 
  }
 
J

Jens.Toerring

lynology said:
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
 
J

Joona I Palaste

(e-mail address removed)-berlin.de scribbled the following:
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.
 
J

Jens.Toerring

Joona I Palaste said:
(e-mail address removed)-berlin.de scribbled the following:
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
 
C

Chris Torek

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top