By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

Bad File Descriptor Error on strcat/strcpy

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. if (strlen(cdukeypad->Scrpad) > 0){
  2. printf("Character received is %s.\n", cdukeypad->Scrpad);
  3. if (strlen(CG_cdukeypad_CHA.Scrpad) < 25){
  4. strcat(CG_cdukeypad_CHA.Scrpad, cdukeypad->Scrpad);
  5. }
  6. if (fflush(NULL) == EOF){
  7. VP_INFO(("Error writing text to Scratchpad.\n"));
  8. VP_INFO(("Error is %s\n", strerror(errno)));
  9. }
  10. }
  11.  
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
lynology <ya*********@lmco.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

P: n/a
Je***********@physik.fu-berlin.de scribbled the following:
lynology <ya*********@lmco.com> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #3

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Je***********@physik.fu-berlin.de scribbled the following:
lynology <ya*********@lmco.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

P: n/a
In article <news:1b*************************@posting.google.c om>
lynology <ya*********@lmco.com> 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 (4039.22'N, 11150.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.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.