read()


  XL C/C++ for z/VM: Runtime Library Reference
Previous topic | Next topic | Contents | Index | Contact z/VM | Library | PDF


creat() — Create a New File or Rewrite an Existing One

XL C/C++ for z/VM: Runtime Library Reference
SC09-7624-04

Standards/Extensions     C or C++     Dependencies
POSIX.1
XPG4
XPG4.2     both     

Format

#define _OPEN_SYS
#include <fcntl.h>

int creat(const char *pathname, mode_t mode);

General Description

The function call: creat(pathname,mode) is equivalent to the call:

   open(pathname, O_CREAT¦O_WRONLY¦O_TRUNC, mode);

Thus, the file named by pathname is created, unless it already exists. The file is then opened for writing only, and is truncated to zero length. For further information, see open() — Open a File.

The mode argument specifies the file permission bits for creating the file. You can use the following symbols for a mode:

S_ISUID
    Privilege to set the user ID (UID) for execution. When this file is run through an exec function, the effective user ID of the process is set to the owner of the file, so that the process has the same authority as the file owner rather than the authority of the actual invoker.
S_ISGID
    Privilege to set group ID (GID) for execution. When this file is run through an exec function, the effective group ID of the process is set to the group ID of the file, so that the process has the same authority as the file owner rather than the authority of the actual invoker.
S_ISVTX
    Indicates shared text. Keep loaded as an executable file in storage.
S_IRUSR
    Read permission for the file owner.
S_IWUSR
    Write permission for the file owner.
S_IXUSR
    Search permission (for a directory) or execute permission (for a file) for the file owner.
S_IRWXU
    Read, write, and search, or execute, for the file owner; S_IRWXG is the bitwise inclusive OR of S_IRUSR, S_IWUSR, and S_IXUSR.
S_IRGRP
    Read permission for the file’s group.
S_IWGRP
    Write permission for the file’s group.
S_IXGRP
    Search permission (for a directory) or execute permission (for a file) for the file's group.
S_IRWXG
    Read, write, and search, or execute permission for the file's group. S_IRWXG is the bitwise inclusive OR of S_IRGRP, S_IWGRP, and S_IXGRP.
S_IROTH
    Read permission for users other than the file owner.
S_IWOTH
    Write permission for users other than the file owner.
S_IXOTH
    Search permission for a directory, or execute permission for a file, for users other than the file owner.
S_IRWXO
    Read, write, and search, or execute permission for users other than the file owner. S_IRWXO is the bitwise inclusive OR of S_IROTH, S_IWOTH, and S_IXOTH.

Returned Value

If successful, creat() returns a file descriptor for the open file.

If unsuccessful, creat() returns a value of -1 and sets errno to one or the following values under the conditions described:

EACCES
    A file descriptor was not created for one of these reasons:

        The process did not have search permission on a component in pathname.
        The file exists but the process did not have appropriate permissions to open the file in the way specified by the flags.
        The file does not exist, and the process does not have write permission on the directory where the file is to be created.
        O_TRUNC was specified, but the process does not have write permission on the file.

EINTR
    The open() function was interrupted by a signal.
EISDIR
    pathname is a directory, and options specifies write or read/write access.
ELOOP
    A loop exists in symbolic links. The creat() function issues this error if it detects the number of symbolic links detected in the resolution of pathname is greater than POSIX_SYMLOOP.
EMFILE
    The process has reached the maximum number of file descriptors it can have open.
ENAMETOOLONG
    pathname is longer than PATH_MAX characters or some component of pathname is longer than NAME_MAX characters while _POSIX_NO_TRUNC is in effect. For symbolic links, this error occurs if the length of a pathname string substituted for a symbolic link in the pathname argument exceeds PATH_MAX. The PATH_MAX and NAME_MAX values can be determined with pathconf().
ENFILE
    The system has reached the maximum number of file descriptors it can have open.
ENOENT
    O_CREAT is specified, and either the path prefix does not exist or the pathname argument is an empty string.
ENOSPC
    The directory or file system intended to hold a new file has insufficient space.
ENOTDIR
    A component of pathname is not a directory.
EROFS
    pathname is on a read-only file system.

Example
CBC3BC28

/* CBC3BC28
   This example creates a file.
 */
#define _OPEN_SYS
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#undef _OPEN_SYS
#include <stdio.h>

main() {
  char fn[]="creat.file", text[]="This is a test";
  int fd;

  if ((fd = creat(fn, S_IRUSR ¦ S_IWUSR)) < 0)
    perror("creat() error");
  else {
    write(fd, text, strlen(text));
    close(fd);
    unlink(fn);
  }
}

Related Information

    fcntl.h
    sys/stat.h
    sys/types.h
    close() — Close a File
    open() — Open a File
    unlink() — Remove a Directory Entry

Go to the previous page Go to the next page

Notices | Terms of use | Support | Contact z/VM

URL:  http://publib.boulder.ibm.com/infocenter/zvm/v6r1/topic/com.ibm.zvm.v610.edclv/rtcre.htm

Copyright IBM Corporation 1990, 2010

This information center is Built on Eclipse™ ( www.eclipse.org ).

从文件中读取指定大小的字节函数read()
 
语法: ssize_t read(int fd,void *buf,int count)
 
说明:
 
         read函数从指定的打开的文件fd中读取指定大小count的字节到从buf开始的缓冲
 
区中.
 
 返回值:若读取失败则返回-1.读取成功则返回实际读取到的字节数,有两种情况:[1].读
 
取到的字节数小于count,这是在读取的文件的总字节数小于count.[2].若读取到的字节
 
数等于count,则在读取的文件的总字节数不小于count时发生.
 
注意:读取到的字节存放在buf缓冲区中,必须最后加上一个字节'\0'才能组成一个字符
 
串.
 
实例:
 
         #include<stdio.h>
         #include<stdlib.h>
         #include<fcntl.h>
         #include<unistd.h>
         #define SIZE 1000
 
         int main(int argc,char *argv[])
         {
               int fd,size;
               char buf[1024];
               if(argc<2)
               {
                      printf("Usage:%s Filename\n",argv[0]);
                      exit(1);
               }
               fd=open(argv[1],O_RDONLY);
               if(fd<0)
               {
                      printf("Fail to open file\n");
                      exit(2);
                 }
                else
                 {
                       size=read(fd,buf,SIZE);
                       if(size<0)
                        {
                                printf("Fail to read file\n");
                                exit(3);
                        }
                       else
                       {
                                printf("From File %s Read %d bytes\n",argv[1],size);
                                buf[size]='\0';
                                printf(buf);
                        }
                       close(fd);
                   }
                   return 0;
         }
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值