473,722 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ 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_CH A.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_CH A.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 5989
lynology <ya*********@lm co.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(cdukeyp ad->Scrpad) > 0){
printf("Charact er received is %s.\n", cdukeypad->Scrpad);
if (strlen(CG_cduk eypad_CHA.Scrpa d) < 25){
strcat(CG_cduke ypad_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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
Je***********@p hysik.fu-berlin.de scribbled the following:
lynology <ya*********@lm co.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(cdukeyp ad->Scrpad) > 0){
printf("Charact er received is %s.\n", cdukeypad->Scrpad);
if (strlen(CG_cduk eypad_CHA.Scrpa d) < 25){
strcat(CG_cduke ypad_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.hel sinki.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.hel sinki.fi> wrote:
Je***********@p hysik.fu-berlin.de scribbled the following:
lynology <ya*********@lm co.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(cdukeyp ad->Scrpad) > 0){
printf("Charact er received is %s.\n", cdukeypad->Scrpad);
if (strlen(CG_cduk eypad_CHA.Scrpa d) < 25){
strcat(CG_cduke ypad_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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
In article <news:1b******* *************** ***@posting.goo gle.com>
lynology <ya*********@lm co.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(cdukeyp ad->Scrpad) > 0){
printf("Charact er received is %s.\n", cdukeypad->Scrpad);
if (strlen(CG_cduk eypad_CHA.Scrpa d) < 25){
strcat(CG_cduke ypad_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
5587
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> #include<stdlib.h> int main() {
23
11609
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 "c is abcd" . #include <stdio.h> #include <string.h> void main() { char a="ab"; char b="cd";
18
21882
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 given as an argument to the program. I wrote a small test program that doesn't work because strcmp() fails to find a matching line. Here's the code: #include <stdio.h> #include <string.h> int main(int argc, char** argv)
9
12786
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
2062
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 compiles Ok and I created a shared library trigger.so. But when I load it into pgAdmin it tells me 'An error had occured'. To address that I put here the source code so you can check it out: (note: it has a oid (blob) field the row, and that could be...
8
539
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 one field. To do this I tried to concatenate those 3 numbers into one using strcat. A piece of code is as follows /*function to enter the date*/ char * date (void) {
3
16822
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 *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
0
30995
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 accordingly to the database and then after that, those files need to be removed to another folder. This program then will be executed again once it receive another 3 files on the next hour. The reason why I need to remove those files to another folder is...
65
5080
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 got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
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 we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.