Thread: Bad file discriptor !!!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    France
    Posts
    6

    Bad file discriptor !!!

    Hello
    I have this code:

    Code:
    void prtastr(const char *buf, int fd,int n){
    	int i,err;
    	char* s;
    	if ((s=calloc(strlen(buf)+1,sizeof (char)))==NULL) 
    		perror("couldn't copy buffer");
    		
    	strcpy(s,buf);
    	//fprintf(stderr,s);
    	while (s!=NULL){
    		while(((err=write(fd,s,1))==-1)&&(errno==EINTR));
    		if(err==-1)perror("An error has occured");
    		s++;
    	}		
    }
    It is working just fine with fprintf , i want to print it character by character .. but it is giving me An error has occured : Bad file discriptor


    I don't know what is wrong with my file discriptor i am calling it using :
    (int) stderr

    All help are apreciated

  2. #2
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by husust
    Hello
    I have this code:

    Code:
    void prtastr(const char *buf, int fd,int n){
    	int i,err;
    	char* s;
    	if ((s=calloc(strlen(buf)+1,sizeof (char)))==NULL) 
    		perror("couldn't copy buffer");
    		
    	strcpy(s,buf);
    	//fprintf(stderr,s);
    	while (s!=NULL){
    		while(((err=write(fd,s,1))==-1)&&(errno==EINTR));
    		if(err==-1)perror("An error has occured");
    		s++;
    	}		
    }
    It is working just fine with fprintf , i want to print it character by character .. but it is giving me An error has occured : Bad file discriptor


    I don't know what is wrong with my file discriptor i am calling it using :
    (int) stderr

    All help are apreciated

    Well, assuming you're on a Unix/Linux system, I can tell you this:

    stderr is declared like this in stdio.h:

    extern FILE *stderr;

    That means that stderr is really a pointer to a FILE.

    You are trying to pass it to write, which expects an integer file descriptor. Obviously a pointer to FILE is not an integer file descriptor.

    I'm not positive, and it may well be implementation specific, but I believe that stdin, stdout and stderr are fd's 0, 1, 2 on most Unix systems......

    This should get you past this problem, at any rate.

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    Additional info

    Quote Originally Posted by husust
    Code:
    	while (s!=NULL){
    		while(((err=write(fd,s,1))==-1)&&(errno==EINTR));
    		if(err==-1)perror("An error has occured");
    		s++;
    	}
    The loop control is wrong.

    If s points to a valid bit of memory at the beginning (non-NULL), you're likely to go through this loop a very long time before it wraps around to all 0s (that is, being a NULL pointer).

    What you really want here is
    Code:
    while (*s != '\0') {
    This will get you out at the end of the string.

    Even if you see an error in write() (other han EINTR), you're going to continue to try to write the rest of the characters. You may want to exit the outer loop when you have an error, after the call to perror(), of course.

    Of course, none of what I said answers why you're getting a bad file descriptor error, fgw_three had the answer to that already. The function fileno(3) will return a file descriptor (int) associated with an open FILE pointer (FILE *), such as stderr.
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    France
    Posts
    6
    Hello again,
    I thank both of you for your help. I think that I should be able to solve the problem now. But Idon't get exactly what you said in the end filker0

    " The function fileno(3) will return a file descriptor (int) associated with an open FILE pointer (FILE *), such as stderr. "

    One more thing, I already solved the problem of the file descriptor thing by using fwrite which works with FILE* ( and not with file descriptors) . What i am trying to teste here ( and learn off course) is to to know how to work with file descriptors.

    A file descriptor in an interger, So how should i do to use it?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    But Idon't get exactly what you said in the end filker0

    " The function fileno(3) will return a file descriptor (int) associated with an open FILE pointer (FILE *), such as stderr. "
    That means enter
    Code:
    man 3 fileno
    in a terminal and read the description of the function
    Kurt

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    France
    Posts
    6
    Yes I guess that this is what i need. I will check it out

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by husust
    Hello again,
    I thank both of you for your help. I think that I should be able to solve the problem now. But Idon't get exactly what you said in the end filker0

    " The function fileno(3) will return a file descriptor (int) associated with an open FILE pointer (FILE *), such as stderr. "

    One more thing, I already solved the problem of the file descriptor thing by using fwrite which works with FILE* ( and not with file descriptors) . What i am trying to teste here ( and learn off course) is to to know how to work with file descriptors.

    A file descriptor in an interger, So how should i do to use it?

    Okay, this is all from memory, but here goes:

    The standard library functions, such as fwrite, fread, fprintf etc, use a pointer to a FILE "object", sometimes referred to as a stream. You can get the declarations for those functions in stdio.h

    The "Unix" functions, such as read, write, use a file descriptor, which is an int.
    The declarations are in unistd.h

    The file descriptor an index into the process's table of open file descriptors, which contains pointers into a table that the kernel maintains for all open files.

    It's been quite a while since I dug into Unix Internals, but that is kind of the way I remember it.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    stderr is defined in <stdio.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM