复习:
IO操作:打开-读写-关闭
文件操作
文件IO:open -read- write-close-perror-lseek —fd
标准IO:fopen(r w a)-fclose-fgets-fread-fwrite-fputs-!feof(fp) —FILE *
fgetc-fseek-rewind-fputc-ftell
目录操作
新知识:
一、目录IO
1.
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
功能:打开目录流
参数:要打开的目录 包括路径
返回值:成功返回目录流指针 失败返回NULL
-
#include <dirent.h> struct dirent *readdir(DIR *dirp);
功能:读取目录
返回值:成功struct dirent类型指针 到达结尾或失败返回NULLstruct dirent {
ino_t d_ino; /* inode number /inode编号 文件编号
off_t d_off; / offset to the next dirent /
unsigned short d_reclen; / length of this record /
unsigned char d_type; / type of file; not supported
by all file system types /
char d_name[256]; / filename */ 文件名
};//dp = opendir(".")
//
while(1)
{
sdt = readdir(dp)
if()
{break;}
sdt->d_name
} -
#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp); 功能:关闭目录 ////////////////////////////////////////// #include"my.h" int main() { //打开当前目录 DIR *pd = opendir("."); if(NULL==pd) { perror("opendir"); return 0; } struct dirent *pread=NULL; while(1) { //读取目录pd pread = readdir(pd); if(NULL==pread)//说明读到目录结尾 { break; } //输出读取到的文件名 printf("%s\n",pread->d_name); } //关闭目录 closedir(pd); return 0; }
练习:打印当前目录下所有文件名,不包括隐藏文件
//////////////////////////////////////////////
#include"my.h"
int main()
{
struct dirent *pret = NULL;
//打开当前目录
DIR *pdir = opendir(".");
if(NULL==pdir)
{
perror("opendir");
return 0;
}
while(1)
{
//读取目录 读到一个文件信息
pret = readdir(pdir);
if(pret==NULL)
{
break;
}
if(pret->d_name[0]=='.')//遇到.开头的文件
{
continue;//进入下一次循环
}
printf("%s ",pret->d_name);
}
closedir(pdir);
return 0;
}
4.文件属性
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);//stat("a.txt",&s);
功能:获取path文件属性 将其保存到buf 不能获取链接文件属性
返回值:成功0 失败-1
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */ inode 文件编号
mode_t st_mode; /* protection */文件操作权限 文件类型
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */ 文件大小
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
//////////////////
实例:读取一个文件的属性
#include"my.h"
int main()
{
struct stat s;
//获取my.h文件属性
int ret = stat("my.h",&s);
if(ret<0)
{
perror("stat");
return 0;
}
//输出my.h大小
printf("%ld\n",s.st_size);
return 0;
}
5.获取文件类型 m代表st_mode
S_ISREG(m) is it a regular file? //如果该文件是常规文件 则宏返回真 否则返回假
S_ISDIR(m) directory? //目录文件
S_ISCHR(m) character device? //字符设备文件
S_ISBLK(m) block device?//块设备文件
S_ISFIFO(m) FIFO (named pipe)?//管道
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)//软链接文件
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)//套接字文件
代码:
#include"my.h"
int main()
{
struct stat s;//
//1.获取文件属性
int ret = stat("/home",&s);
if(ret<0)
{
perror("stat");
return 0;
}
if(S_ISREG(s.st_mode))//说明当前文件是常规文件
{
puts("-");
}
else if(S_ISDIR(s.st_mode))//说明当前文件是目录文件
{
puts("d");
}
return 0;
}
进程系统调用
一、进程查看及创建
-
#include <sys/types.h>
#include <unistd.h>pid_t getpid(void);//pid 是int别名 功能:获取当前调用进程PID pid_t getppid(void); 功能:获取当前调用进程的父进程的PID ////////////////////////////////////// #include<stdio.h> int main() { printf("pid:%d\n",getpid());//获取当前调用进程PID printf("ppid:%d\n",getppid());//获取父进程PID while(1); return 0; }
#include <unistd.h>
pid_t fork(void);
功能:创建新进程
返回值:成功调用返回两次 在父进程中返回子进程PID 在子进程中返回0
出错返回-1
/////////////////////////////////////////
#include"my.h"
int main()
{
//创建子进程:创建后 有两个进程 一个是main所在进程 另一个是新创建的子进程
fork();
printf("good good study!\n");//打印两次 说明有两个进程调用printf
return 0;
}
3.如何区分两个进程 :返回值
#include"my.h"
int main()
{
printf(“main:%d\n”,getpid());
//创建子进程:创建后 有两个进程 一个是main所在进程 另一个是新创建的子进程
pid_t id = fork();//pid <0 >0 =0
if(id>0)//返回的是子进程PID 说明在父进程中
{
sleep(1);//本进程睡眠1s
printf(“father:%d\n”,getpid());
}
else if(0==id)//说明在子进程中
{
printf(“Baby:%d father:%d\n”,getpid(),getppid());
}
else //<0 出错
{
perror(“fork”);
return 0;
}
return 0;
}