Thread: I have a problem in Linux programming about file read and write

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

    Red face I have a problem in Linux programming about file read and write

    If file is not exist,create it and write a string into it, else write a string into this file.

    but the problem is that open() return a bad file descriptor if this file is exist.

    code:

    Code:
    /*
    Linux Open() example source code
    Author:        Yucoat(www.yucoat.com)
    Data:        2011-10-31
    Version:    v0.1
    ChangeLog:    Create This program        2011-20-31
     */
    #include <fcntl.h>        //for open
    #include <errno.h>        //for errno
    #include <stdio.h>
    
    
    int main()
    {
        int fd;
    
    
        if ((fd = open(    "./Name",                                        //File name
                        O_WRONLY|O_EXCL|O_APPEND|O_CREAT,                //Mod
                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH)) < 0)    //Access
        {
            perror("open");
            return -1;
        }
    
    
        if (write(fd, "Yucoat\n", 7) != 7)
            perror("Write");
        close (fd);
        return 0;
    }
    Thx for every body!I'm very sorry for my bad english!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by thlgood View Post
    but the problem is that open() return a bad file descriptor if this file is exist.
    Of course it does. You told it to:
    Quote Originally Posted by thlgood View Post
    Code:
        if ((fd = open(    "./Name",                                        //File name
                        O_WRONLY|O_EXCL|O_APPEND|O_CREAT,                //Mod
    When in doubt, read the man page.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    China
    Posts
    9
    Yes I read it.

    But it's a big challenge for me to understand some man pages...

    Thx

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This
    O_EXCL error if create and file exists
    seems pretty crystal clear to me, considering your problem is
    the problem is that open() return a bad file descriptor if this file is exist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Programming, Write-Read Issue
    By a5k3r in forum Linux Programming
    Replies: 1
    Last Post: 07-30-2010, 03:55 PM
  2. read write to file problem
    By xniinja in forum C Programming
    Replies: 1
    Last Post: 06-09-2010, 11:01 AM
  3. read and write image using C programming
    By SHCS in forum C Programming
    Replies: 2
    Last Post: 05-16-2010, 11:03 AM
  4. Linux SCSI read/write
    By galapogos in forum Linux Programming
    Replies: 1
    Last Post: 12-09-2008, 10:36 AM
  5. what should i do when .....(read/write txt file problem)
    By Jasonymk in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2003, 08:55 PM

Tags for this Thread