logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

Unix C/C++ Input and Output Function Reference

by digipine posted Nov 01, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
Unix, C, and C++
Function Reference
Unix Input and Output
 
 
  int open(char * filename, int flags)
  int open(char * filename, int flags, int mode)
      include: <fcntl.h>
      flags = bitwise | or of any of the following:
             O_RDONLY        Only read operations permitted
             O_WRONLY        Only write operations permitted
             O_RDWR          Read and Write operations both permitted
             O_NONBLOCK      Non-blocking, applies to open operation only
             O_APPEND        All writes go to end of file
             O_CREAT         Create file if it doesn't already exist
             O_TRUNC         Delete existing contents of file
             O_EXCL          Open fails if file already exists
             O_SHLOCK        Get a "shared lock" on the file
             O_EXLOCK        Get an "exclusive lock" on the file
             O_DIRECT        Try to avoid all caching of operations
             O_FSYNC         All writes immediately effective, no buffering
             O_NOFOLLOW      If file is symbolic link, open it, don't follow it
      mode required if file is created, ignored otherwise.
           mode specifies the protection bits, e.g. 0644 = rw-r--r--
      returns <0 for error, or integer file descriptor.
 
 
  int close(int fd)
      include: <unistd.h>
      fd = file descriptor as returned by open
      returns <0 for error, 0 for success.
 
 
  int read(int fd, void * ptr, int numbytes)
      include: <unistd.h>
      fd = file descriptor as returned by open
      ptr = address in memory where data is to be stored;
            may be a pointer of any type.
      numbytes = number of bytes to attempt to read
      returns <0 for error, 0 for end-of-file,
              or number of bytes successfully read.
              (for a non-blocking interactive file, that may be zero).
 
 
  int write(int fd, void * ptr, int numbytes)
      include: <unistd.h>
      fd = file descriptor as returned by open
      ptr = address in memory where data already is;
            may be a pointer of any type.
      numbytes = number of bytes to attempt to write
      returns <=0 for error,
              or number of bytes successfully written.
              (for a non-blocking file, that may be less than the number attempted
               without any errors having occurred).
 
 
  int lseek(int fd, int position, int startpoint)
      include: <unistd.h>
      Sets the file position effective for the next read or write operation.
      fd = file descriptor as returned by open
      position = position within file:
            number of bytes between startpoint and the desired position, may be negative.
      startpoint = location in file that position is relative to, one of:
             SEEK_SET        Position is number of bytes from beginning of file
             SEEK_END        Position is number of bytes after end of file
             SEEK_CUR        Position is number of bytes after current position
      returns <0 for error,
              or resulting file position relative to beginning of file.
      Moving beyond the end of the file is permitted; the file is extended in length
      only if a write occurs beyond the current end of file. Moveing before the
      beginning of a file is not permitted. A write operation after a move only
      overwrites the number of bytes actually written.
 
 
  int stat(char * file, struct stat * info)
      include: <sys/types.h>
      include: <sys/stat.h>
      Finds information about a file.
      file = The name (or path) for the file.
      info = A pointer to an uninitialised stat structure.
            Even in C++, the type must be "struct stat *" because of the name clash.
            Information about the file is stored in the info object.
      returns <0 for error (including file does not exist),
              or 0  for success.
      The useful fields of a stat object are as follows. All have types that behave like
      ints, although they may be 16, 32, or 64 bits long.
             st_size              size in bytes
             st_ino               inode number
             st_mode              protection or mode (see below)
             st_nlink             number of hard links
             st_uid               file's owner's identification number
             st_gid               file's group identification number
             st_birthtime         date and time of file creation (see below)
             st_mtime             date and time of last modification (see below)
             st_atime             date and time of last access (see below)
      All times are stored as time_t values, as described here.
      Mode and Protection. st_mode is the logical-or of a number of bits each representing
      different properties. The named constants for these bits, and their values in octal are:
             S_ISUID   0004000    set user id on execution
             S_IRUSR   0000400    protection: readable by owner
             S_IWUSR   0000200                writable by owner
             S_IXUSR   0000100                executable by owner
             S_IRGRP   0000040                readable by group
             S_IWGRP   0000020                writable by group
             S_IXGRP   0000010                executable by group
             S_IROTH   0000004                readable by all
             S_IWOTH   0000002                writable by all
             S_IXOTH   0000001                executable by all
      Four bits of the mode give the file type. The mask for type is S_IFMT = 0170000
          if (mode & S_IFMT) is S_IFREG = 0100000,  type = perfectly ordinary file
          if (mode & S_IFMT) is S_IFDIR = 0040000,  type = directory
          if (mode & S_IFMT) is S_IFLNK = 0120000,  type = symbolic link
          if (mode & S_IFMT) is S_IFIFO = 0010000,  type = named pipe
          if (mode & S_IFMT) is S_IFSOCK = 0140000,  type = named socket
 
TAG •

List of Articles
No. Subject Author Date Views
23 [Linux] Pthread 사용법, Thread 동기화 총정리 digipine 2017.11.01 294049
» Unix C/C++ Input and Output Function Reference digipine 2017.11.01 88074
21 Solaris에서 pmap을 이용하여 백그라운드 프로세스 메모리 크기 구하기 digipine 2017.10.29 28598
20 Linux C 언어로 Shell 명령어 실행하기 digipine 2017.11.01 22587
19 C++ 컴파일 오류(error): variable 'std::istringstream sstream' has initializer but incomplete type digipine 2017.11.02 21077
18 wchar_t에 대하여 digipine 2017.11.01 7343
17 [shared lib] so 동적 라이브러리 만들기와 사용법 - 리눅스 digipine 2017.11.01 6434
16 STL MAP 예제로 공부하기 digipine 2017.10.29 5204
15 fopen 파일 열기 모드 옵션 정리 digipine 2017.11.02 3894
14 make -j 옵션으로 컴파일 속도 최적화 하기 digipine 2017.11.01 2759
13 C++에서 extern의 역할, 기능 digipine 2017.10.29 2656
12 MD5 파일 변조 검사 관련 소스 (리눅스/Windows) digipine 2017.10.29 2613
11 Callback in C++ 와 Delegate 차이점 digipine 2017.11.01 2525
10 [C/C++] 현재시간 구하기 digipine 2017.10.28 2212
9 C++ Atomic 클래스에 대해서 file 엉뚱도마뱀 2017.12.13 2139
8 C 에서 Overloading 구현 digipine 2017.11.01 1790
7 brute-force 알고리즘을 이용한 패턴 위치 찾기 digipine 2017.10.29 1501
6 [linux] zlib build 방법 digipine 2017.11.01 1483
5 소켓 통신을 이용한 HTTP 서버 개발 강의록 file digipine 2020.08.01 1482
4 [C/C++] Random UUID String 생성 코드 digipine 2021.10.21 1302
Board Pagination Prev 1 2 Next
/ 2