Linux 用 /,Windows 用 \。\ 與 W 形狀相似 → Windows;Linux 是反方向,好記。
#include <sys/types.h>, <sys/stat.h>, <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
O_RDONLY、O_WRONLY、O_RDWR、O_CREAT、O_TRUNC、O_APPEND 等組合。-1 失敗;≥0 檔案描述符(0=stdin, 1=stdout, 2=stderr,進程分配從 3 起)。#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
size_t write(int fd, const void *buf, size_t count);
int close(int fd);
int creat(const char *pathname, mode_t mode);
#include <unistd.h>, <fcntl.h>, <stdio.h>, <stdlib.h>
int main() {
int fd = open("show_read.c", O_RDONLY);
char buf[1024];
int n = read(fd, buf, sizeof(buf));
close(fd);
write(STDOUT_FILENO, buf, n);
return 0;
}
int main() {
char *s = "Linux Programmer!\n";
char buf[1024];
int fd = creat("newfile.txt", 0644); // 建立/清空
fd = open("newfile.txt", O_WRONLY); // 重開以寫入
write(fd, s, strlen(s));
close(fd);
fd = open("newfile.txt", O_RDONLY);
int n = read(fd, buf, sizeof(buf));
close(fd);
write(STDOUT_FILENO, buf, n);
return 0;
}
while ((n = read(src_fd, buf, BUFSIZ)) > 0)
write(dst_fd, buf, n);
搭配 open 來源 O_RDONLY、目的 O_WRONLY|O_CREAT|O_TRUNC 即可實現基本複製。
微軟延長 Windows Vista 與 Windows 7 生命週期
Linux source 指令詳解:在當前 Shell 執行腳本與環境變數傳遞
三步製作 Linux 開機隨身碟:使用 UNetbootin 輕鬆建立救援與安裝媒體
Linux 文件檢視與管理核心命令大全:cat、less、head、tail、touch、umask、chattr、find 實戰
Logrotate 完全指南:Linux 日誌輪替、壓縮與自動化管理最佳實踐
Linux 上網配置經典兩式:共享 Windows 主機上網與 ADSL 直撥完整參數