Thread: error in creat file by using 'open' function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    China Guangzhou
    Posts
    7

    Unhappy error in creat file by using 'open' function

    hi I am just write a simple program(c.c) :
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    int main()
    {
       int fp;
       char * pathname = "/dream/dream.txt";
       if((fp=open(pathname,O_WRONLY))==-1)
    	printf("error in open file\n");
       write(fp,pathname,11);
       close(fp);
       return 0;
    }
    I can build it with gcc -o c c.c,it 's no error but when exec it ,it can't write the string in the file 'dream.txt' . when i rebuild it with 'gcc -Wall -o c c.c',It print that:
    Code:
    c.c: In function ‘main’:
    c.c:14: warning: implicit declaration of function ‘write’
    c.c:15: warning: implicit declaration of function ‘close’
    can you tell me why?
    Thanks !!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. open google and write
    "man write"
    2. open the first link to the man page like http://www.manpagez.com/man/2/write/
    3. read what include file is needed to use this function
    4. include this file into your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    O_WRONLY is not sufficient to create a file. You need "O_WRONLY | O_CREAT", and you need to pass something valid in the third argument of open()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    China Guangzhou
    Posts
    7
    Thank you very much!!!
    I did it with what you said ,and it run!!

    said again :Thank you!!!
    Last edited by liuguobiao; 03-23-2009 at 10:37 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Another problem: you didn't allocate any space to pathname:
    Quote Originally Posted by liuguobiao View Post
    hi I am just write a simple program(c.c) :
    Code:
       char * pathname = "/dream/dream.txt";
    Should be:
    Code:
      char * pathname[] = "/dream/dream.txt";
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    Another problem: you didn't allocate any space to pathname:

    Should be:
    Code:
      char * pathname[] = "/dream/dream.txt";
    And why should it be?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function to open file?
    By Abda92 in forum C Programming
    Replies: 1
    Last Post: 10-27-2007, 07:50 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM