473,466 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bad File Descriptor Error on strcat/strcpy

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
4 5969
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
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
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
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 (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.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mahesh | last post by:
Dear friends, In the following code, I can't read the float value from file(temp.txt). Run-time error is coming. I am using Visual C++. #include<stdio.h> #include<math.h> #include<string.h>...
23
by: JC | last post by:
hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can't get the result i want.. i want to get the result with...
18
by: William Payne | last post by:
Hello, I need to write a program that opens a text file and scans the entire file for a specific line and when that line is found, a particular word on that line is to be replaced. The new word is...
9
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
2
by: Juan Jose Costello Levien | last post by:
Hello, I am trying to use a trigger function I wrote in C. Basically what I want to do is to audit a table when a row is inserted into another table by copying the row to the new table. It...
8
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in...
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
0
by: whitemoss | last post by:
Hi All, I had written a code to read a file and insert it's contents to the database. Since I will receive 3 files every hour, so, this program should read those files and insert the contents...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.