error in creat file by using 'open' function

This is a discussion on error in creat file by using 'open' function within the Linux Programming forums, part of the Platform Specific Boards category; 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> ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    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 11: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 08:50 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10: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, 06:38 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 05:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21