Linux支持各种各样的文件系统格式,如ext2、ext3、reiserfs、FAT、NTFS、iso9660等等,不同的磁盘分区、光盘或其它存储设备都有不同的文件系统格式,然而这些文件系统都可以mount到某个目录下,使我们看到一个统一的目录树,各种文件系统上的目录和文件我们用ls命令看起来是一样的,读写操作用起来也都是一样的,这是怎么做到的呢?Linux内核在各种不同的文件系统格式之上做了一个抽象层,使得文件、目录、读写访问等概念成为抽象层的概念,因此各种文件系统看起来用起来都一样,这个抽象层称为虚拟文件系统(VFS,Virtual Filesystem)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
//open file
if(argc<2){
printf("please input filename\n");
exit(1);
}else{
int fd;
umask(0000);
fd = open(argv[1], O_RDWR|O_CREAT, 0666);
if(fd < -1){
printf("error\n");
exit(1);
}else{
printf("success=%d\n", fd);
close(fd);
printf("closed\n");
}
}
return 0;
}读文件(写文件的过程和读文件类似)#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
//open file
if(argc<2){
printf("please input filename\n");
exit(1);
}else{
int fd;
umask(0000);
fd = open(argv[1], O_RDWR|O_CREAT, 0666);
if(fd < -1){
printf("error\n");
exit(1);
}else{
printf("success=%d\n", fd);
char buf[1024];
memset(buf, 0, 1024);
int returnum = read(fd, buf, 1024);
if(returnum != -1){
printf("buf=%s\n", buf);
}else{
printf("read error\n");
exit(1);
}
close(fd);
printf("closed\n");
}
}
return 0;
}原文:http://blog.csdn.net/dawanganban/article/details/38797369