Thread: Linux programming problem Bad file discriptor

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    China
    Posts
    9

    Question Linux programming problem Bad file discriptor

    This is my code
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    char *StrOne = "This is String One\n";
    char *StrTwo = "This is string Two\n";
    
    
    int main(int argc, char *argv[])
    {
        int fd;
        if (argc != 2)
            printf("_______@_@______\n\n");
        else
        {
            if ((fd = open(argv[1], O_CREAT|O_EXCL, S_IRUSR|S_IWUSR)) == -1)
            {
                perror("Open");
                exit (2);
            }
            if (write(fd, StrOne, strlen(StrOne)) != strlen(StrOne))
            {
                perror("Write\n");
                exit (1);
            }
            lseek(fd, SEEK_SET, 1000);
            if (write(fd, StrTwo, strlen(StrTwo)) != strlen(StrTwo))
            {
                perror("Write\n");
                exit (1);
            }
            close (fd);
        }
        return 0;
    }
    this is result:

    Code:
    coat@linux:~/codetest/lseek_null$ ./a.out Nds
    Write: Bad file descriptor
    Thx you every body
    三克油矮里八地。。。。
    Last edited by thlgood; 11-06-2011 at 08:46 AM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You forgot to specify a mode, e.g. O_WRONLY.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    China
    Posts
    9
    It works thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2011, 04:33 AM
  2. Linux ser comm file descriptor problem...
    By kievari in forum C Programming
    Replies: 1
    Last Post: 04-09-2010, 10:51 AM
  3. Bad file discriptor !!!
    By husust in forum C Programming
    Replies: 7
    Last Post: 02-04-2006, 05:38 PM
  4. programming using linux
    By studentc in forum C Programming
    Replies: 9
    Last Post: 06-12-2004, 09:43 AM
  5. Windoze programming to linux programming
    By windoze victim in forum Linux Programming
    Replies: 5
    Last Post: 12-05-2002, 02:02 AM