根目录下所有目录存放内容
1. /boot 系统启动相关文件,例内核、引导程序、initrd、grub
3. /dev 设备文件->(块设备、 字符设备)
随机访问 线性访问
4. /etc 包含软件和系统的配置文件
5. /home 家 各用户的家目录(**root家目录在根目录下)
6. /root 管理员的家
7. /lib 库文件 静态库(XX.a) 动态库(xx.so)
/lib/modules(内核模块文件)
8. /media 挂载点目录,(移动设备)
9. /mnt 挂载点目录 (一般额外的临时文件系统)
10. /opt 可选目录,第三方程序安装目录
10./proc 伪文件系统,内核映射文件
11./sys 伪文件系统,跟硬件设备相关的属性映射文件
12./tmp 临时文件
13./var 可变化文件
14./bin 可执行命令文件,供所有用户使用的完成基本维护任务的命令
15./sbin 管理命令
16./usr 只读文件
/usr/local 第三方软件安装目录
常见的文件类型
- - 普通文件
- d 目录
- l 链接文件
- c 字符设备,如键盘,串口
- b 块设备,如硬盘,CDROM
- p 管道文件,缓冲区
- f 堆栈文件
系统调用
linux程序运行空间分为 内核空间和用户空间(逻辑上独立),在进行系统调用时,从用户空间进入内核空间,在返回用户空间
linux系统调用按功能可分为:
- 进程控制
- 进程间通信
- 文件系统控制
- 系统控制
- 存储管理
- 网络管理
- Socket控制
- 用户管理
linux的文件描述符
程序打开文件时,操作系统返回对应的文件描述符(一般从3开始)
0 1 2(表示 stdin stdout stdrr)
有效范围0-OPEN_MAX
open系列系统调用
-
creat:
int creat(const*pathname,mode_t mode);
包含头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
- List item
int main()
{
int fd;
char pathname[100] = {};
char filename[30] = {0};
puts("please input name of file:\n");
scanf ("%s",filename);
sprintf(pathname,"/home/ben/c.d/%s", filename);
fd = creat(pathname, 0755);
printf("-----------1------------a");
if (fd == -1)
{
printf("error:%d:fd\n",fd);
}
else
{
printf("create successful!file is%d\n",fd);
}
return 0;
}
-
open:
int open(const char* pathname,int flags [ ,mode_t mode]);
flags ☞打开文件的方式,有以下几种方式,可用|组合O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR(上三个只能用一个) 可读可写打开 O_APPEND 删除原本文件内容 O_TRUNC 新写内容接续到原内容后 O_APPEND 写时追加到文件尾 O_CREAT 如果文件不存在,则创建新文件 O_NONBLOCK 把默认的阻塞型改为非阻塞(只用于设备文件) mode_t mode 在创建文件时用到,一是open带O_CREAT,还有creat函数
0777 权限全给
包含头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fr,closeState,len,fw;
char readfile[100] = {};
char readname[30] = {0}; //
char writefile [100] = {};
char writename[30] = {0}; //writed file
char buf[100] = {0};
puts("intputs the name of read:\n");
scanf("%s",readname);
sprintf(readfile,"/home/ben/c.d/%s",readname);
fr = open(readfile, O_RDWR, 0755);
if(fr == -1)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
else
{
printf("create successful! file is %d\n",fr);
}
int close(int fd);
closeState = close(fr); //close readfile
if (closeState ==0)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
else if(closeState == -1)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
int write (int fd, const void * buf, size_length)
包含头文件 #include <unistd.h>
把length个字节从buf指向的缓冲区写到文件描述符fd所指向的文件中,返回值为实际写入的字节数
int read(int fd, void *buf, size_length)
从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区,返回值为实际读取字
//读取输入一个源文件的内容,写入到被输入的目标文件
#if 1
int main()
{
int fr, fw,len,cfr,cfw;
char str[100] = {0};
char readname[30] = {0};
char writename[30] = {0};
char readfile[100] = {};
char writefile[100] = {};
puts ("Please input readname:\n");
scanf("%s",readname);
sprintf(readfile, "/home/ben/c.d/%s",readname);
fr = open(readfile, O_RDWR, 0755);
if (fr == -1)
{
printf("error no file,error:%d,error:%s\n",errno,strerror(errno));
}
else
{
printf("The first step :opens the readfile successfully\n");
}
puts("please input writename\n");
scanf("%s",writename);
sprintf(writefile,"/home/ben/c.d/%s",writename);
fw = open(writefile, O_RDWR|O_CREAT, 0755);
if (fw == -1)
{
printf ("errno:%d,error:%s\n",errno, strerror(errno));
}
else
{
printf("The second steo :opens the writefile successfully\n");
}
/*
* while (1)
{
len = read(fr, str, 99);
write(fw, str, len);
if (len < 99)
{
break;
}
}
*/
while ((len = read(fr,str,2)) > 0)
{
write(fw,str,len);
if (len == 0)
break;
}
if (lseek(fr,0,SEEK_END) == lseek(fw,0,SEEK_END))
{
printf("write success\n");
}
else
{
printf("write failure!\n");
}
cfr = close(fr); //close readfile
if (cfr == 0)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
else if(cfr == -1)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
cfw = close(fw); //close writefile
if (cfw == 0)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
else if(cfw == -1)
{
printf("errno:%d,error:%s\n",errno,strerror(errno));
}
return 0;
}
#endif
-
lseek
返回值:
调用成功时返回目前读写位置,距离文件开头多少字节
失败返回-1
默认打开读写位置指向文件头
计算文件长度
ret = lseek (fd,0,SEEK_END) //把文件指针移动到文件尾,返回的偏移量就是文件长度了
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
// offset为正数,向文件尾移动,为负数向文件头移动
- 将读写位置移动到文件头:lseek(int fildes,0,SEEK_SET);
- 将读写位置移动到文件尾:lseel(int fildes,0,SEEK_END);
- 取得当前文件位置lseek (int fildes, 0, SEEK_CUR);