【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
最近学习linux系统下的应用编程,参考书籍是那本称为神书的《Unix环境高级编程》,个人感觉神书不是写给草鞋看的,而是
写给大神看的,如果没有一定的基础那么看这本书可能会感到有些头重脚轻的感觉。我自己就是这样,比方说看进程间通信信号量章
节的时候,开始感觉就很迷糊,因此也就想在这里写一些文字,给和我一样的草鞋分享一些自己的学习经历(算不上经验吧)。
环境: windows7, VMware 9.0
操作系统版本: RHEL 5.5
内核版本: 2.6.18-194.el5
Gcc版本: gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-48) 【2008年7月4日构建的】
【linux草鞋应用编程系列】的系列文章,欢迎批评指正。 欢迎转载,如果您愿意可以添加本系列文章的链接,即本草鞋的在博客园
的链接。
正文中的函数的原型都是通过 man page 查看和复制到,查看的时候如果与这里的不一样,请以查看的为准, 因为不同的内核
版本支持的函数,以及函数的参数可能存在一些出入。
废话少说,下面开始正题。
开篇: 系统调用IO接口与标准IO接口
正文:
NAME
open, creat - open and possibly create a file or device SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> int open(const char *pathname, //要打开的文件的路径和文件名
int flags); //打开方式 int open(const char *pathname, //要打开的文件的路径和文件名
int flags, //打开方式 , 这个格式的调用,表示使用了 O_CREAT 打开方式标志。
mode_t mode); //打开后文件的权限 int creat(const char *pathname, //要创建的文件的路径和文件名
mode_t mode); //创建后文件的权限
mode must be specified when O_CREAT is in the flags, and is ignored otherwise.
creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.
NAME
read - read from a file descriptor SYNOPSIS
#include <unistd.h> ssize_t read( int fd, //要读取文件的文件描述符
void *buf, //读取数据存储的缓冲区
size_t count); //要读取字节数
返回值:
NAME
write - write to a file descriptor SYNOPSIS
#include <unistd.h> ssize_t write(int fd, //要写入文件的文件描述符
const void *buf, //待写入数据的缓冲区
size_t count); //要写入的字节数
返回值:
成功返回写入到字节数, 返回0 表示没有写入任何东西。
失败返回 - 1 .
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> #define BUF_LEN 1024 int main(int argc, char* argv[])
{
int fd_src,
fd_dst;
char buf[BUF_LEN];
int ret;
int ret_r; if(argc < )
{
printf("usage: cpfile file_src file_dst\n");
printf("\tfile_src:file want to copy\n");
printf("\tfile_dst:file where to store\n");
exit();
} fd_src=open(argv[], O_RDONLY);
if(- == fd_src )
{
strcpy(buf,"open ");
strcat(buf,argv[]);
perror(buf);
exit();
}
fd_dst=open(argv[],O_WRONLY|O_CREAT,);
if(- == fd_dst )
{
strcpy(buf,"open ");
strcat(buf,argv[]);
perror(buf);
exit();
} do
{
memset(buf,,sizeof(buf));
ret_r=read(fd_src,buf,sizeof(buf) );
if(- == ret)
{
strcpy(buf,"read ");
strcat(buf,argv[]);
perror(buf);
exit();
}
ret=write(fd_dst,buf,ret_r);
if(- == ret)
{
strcpy(buf,"write ");
strcat(buf,argv[]);
perror(buf);
exit();
}
}while( ret_r != ); close(fd_src);
close(fd_dst);
return ;
}
NAME
opendir - open a directory SYNOPSIS
#include <sys/types.h>
#include <dirent.h> DIR *opendir(const char *name); //要打开的目录的路径和目录名
READDIR() Linux Programmer’s Manual READDIR()
NAME
readdir - read a directory
SYNOPSIS
#include <sys/types.h>
#include <dirent.h> struct dirent *readdir(DIR *dir); //要读取的目录的指针
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */ //文件类型
char d_name[]; /* filename */ //文件名
};
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h> int main(int argc, char* argv[])
{
DIR *dir=NULL;
struct dirent *file=NULL; if(argc < ) //如果没有指定要显示的目录,就显示当前目录的的文件
{
dir=opendir("./");
if(!dir)
{
perror("open");
exit();
}
else
{
while(file=readdir(dir))
printf("%s\t",file->d_name);
}
putchar('\n');
closedir(dir);
exit();
} dir=opendir(argv[]);
if(!dir)
{
perror("open");
exit();
}
while(file=readdir(dir))
printf("%s",file->d_name); printf("\n");
closedir(dir);
return ;
}
[root@localhost ls]# ls
main.c
[root@localhost ls]# gcc -o dir main.c
[root@localhost ls]# ./dir
dir main.c .. .
[root@localhost ls]#
ACCESS() Linux Programmer’s Manual ACCESS()
NAME
access - check user’s permissions for a file
SYNOPSIS
#include <unistd.h> int access( const char *pathname, //要检查的文件路径和文件名
int mode); //要检测的内容,如文件是否存在 F_OK 等
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> #define BUF_LEN 1024 int main(int argc, char* argv[])
{
int fd_src,
fd_dst;
char buf[BUF_LEN];
int ret;
int ret_r; if(argc < ) //参数小于3个,就打印提示信息
{
printf("usage: cpfile file_src file_dst\n");
printf("\tfile_src:which file want to copy\n");
printf("\tfile_dst:file where to store\n");
printf("\n\tIf the file_src and file_dst without path,"
"will operation at current directory\n");
exit();
} //检测目标文件是否存在
if( ! access(argv[],F_OK) )
{
printf("%s exist,do you want to overwrite it?(y/n):",argv[]);
buf[]=getchar();
if( 'n' == buf[] )
exit();
} fd_src=open(argv[], O_RDONLY);
if(- == fd_src )
{
strcpy(buf,"open ");
strcat(buf,argv[]);
perror(buf);
exit();
}
fd_dst=open(argv[],O_WRONLY|O_CREAT,);
if(- == fd_dst )
{
strcpy(buf,"open ");
strcat(buf,argv[]);
perror(buf);
exit();
} do
{
memset(buf,,sizeof(buf));
ret_r=read(fd_src,buf,sizeof(buf) );
if(- == ret)
{
strcpy(buf,"read ");
strcat(buf,argv[]);
perror(buf);
exit();
}
ret=write(fd_dst,buf,ret_r);
if(- == ret)
{
strcpy(buf,"write ");
strcat(buf,argv[]);
perror(buf);
exit();
}
}while( ret_r != ); close(fd_src);
close(fd_dst);
return ;
}
STAT() Linux Programmer’s Manual STAT()
NAME
stat, fstat, lstat - get file status
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> int stat(const char *path, //要查看的文件的路径和文件名
struct stat *buf); //输出参数, 用于存储文件信息的结构体指针 int fstat(int filedes, //打开的文件的文件描述符
struct stat *buf); //输出参数, 用于存储文件信息的结构体指针 int lstat(const char *path, //要查看的文件的路径和文件名
struct stat *buf); //输出参数, 用于存储文件信息的结构体指针
返回值:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */ //硬连接数
uid_t st_uid; /* user ID of owner */ //用户ID
gid_t st_gid; /* group ID of owner */ //组ID
dev_t st_rdev; /* device ID (if special file) */ //特殊文件ID号
off_t st_size; /* total size, in bytes */ //文件大小
blksize_t st_blksize; /* blocksize for filesystem I/O */ //文件IO的块大小
blkcnt_t st_blocks; /* number of 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 <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h> #define BUF_SIZE 512 //定义一个函数解析文件信息
void show_stat(char buf[] ,struct stat f_stat)
{
printf("File : %s\n",buf);
printf("\tuser id: %d\n", f_stat.st_uid);
printf("\tgroup id: %d\n",f_stat.st_gid);
printf("\tfile size:%.3fK\n", . * f_stat.st_size /); //显示3位小数
printf("\tfile ulink:%d\n",f_stat.st_nlink);
} //定义一个函数遍历目录
void dir(const char *path)
{
DIR *dir=NULL; //打开的目录
struct dirent *f_dir=NULL; //存储目录项
char buf[]={};
struct stat f_stat={}; //用来检测文件的信息
int ret=; //打开目录
dir=opendir(path);
if(!dir)
{
strcpy(buf,"acces directory:");
strcat(buf,path);
perror(buf);
exit();
} //遍历目录
while( f_dir = readdir(dir) )
{
//首先获取文件的路径和文件名
strcpy(buf,path); //路径
strcat(buf,"/"); //添加路径分割符号
strcat(buf,f_dir->d_name); //文件名,buf包含路径名和文件名 //获取目录项的属性
ret=stat(buf,&f_stat);
if(ret)
{
perror(buf);
} if(S_ISDIR(f_stat.st_mode)) //如果是目录
{
printf("File : %s\n",f_dir->d_name);
printf("\tA directory\n");
}
else if(S_ISREG(f_stat.st_mode))
{
show_stat(f_dir->d_name, f_stat);
}
else
{
printf("File : %s\n", f_dir->d_name);
printf("\tother file type");
}
}//遍历目录结束
} int main(int argc, char* argv[])
{
struct stat f_stat;
int ret;
char buf[BUF_SIZE]; //首先判断是否有第二个参数, 没有就显示当前目录
if( argc < )
{
dir("."); //注意这个地方,不能传递"./",因为dir函数中会添加最后一个反斜杠
exit(); //显示完成就退出
} //有第二个参数
ret=stat(argv[],&f_stat);
if(ret)
{
strcpy(buf,"access ");
strcat(buf,argv[]);
perror(buf);
exit();
}
if(S_ISDIR(f_stat.st_mode)) //如果是目录
{
dir(argv[]);
}
if(S_ISREG(f_stat.st_mode)) //如果是文件
{
show_stat(argv[],f_stat);
}
return ;
}
CHDIR() Linux Programmer’s Manual CHDIR()
NAME
chdir, fchdir - change working directory
SYNOPSIS
#include <unistd.h> int chdir(const char *path); //要切换到的工作目录
int fchdir(int fd); //通过打开的文件描述符,切换到打开的文件所在的目录
GETCWD() Linux Programmer’s Manual GETCWD()
NAME
getcwd, get_current_dir_name, getwd - Get current working directory SYNOPSIS
#include <unistd.h> char *getcwd( char *buf, //输出函数,用来存储当前路径的缓存区域
size_t size); //缓存区域的大小
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h> //访问错误值代码 extern int errno;
int main(void)
{
char* buf=NULL;
int ret; buf=(char *)malloc(); buf=getcwd(buf,);
if(!buf)
{
if(ERANGE == errno)
buf=(char *)realloc(buf,);
}
buf=getcwd(buf,);
printf("before change directroy: %s\n\n",buf); ret=chdir("/home/volcanol");
if(ret)
{
perror("/home/volcanol");
exit();
}
buf=getcwd(buf,);
printf("after change directory:%s\n\n",buf); free(buf);
return ;
}
#include <stdio.h>
#include <unistd.h> //to use sleep() int main(void)
{
int i=; for(i=;i<;i++)
{
printf("%d ",i);
sleep(); //为了查看效果,才加上sleep();
} printf("\n"); return ;
}
执行的时候,可以看到 0、1、2、3、4 不是一个一个的输出,而是一起输出的。
SETBUF() Linux Programmer’s Manual SETBUF()
NAME
setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
SYNOPSIS
#include <stdio.h> void setbuf(FILE *stream,
char *buf); void setbuffer(FILE *stream,
char *buf,
size_t size); void setlinebuf(FILE *stream); //设置为行缓冲, stream 表示设置缓冲的文件 int setvbuf(FILE *stream, //要缓冲的文件,标准输出为 stdout
char *buf, //缓冲区的首地址, =NULL 表示系统分配,
int mode , //缓冲模式,行缓冲、全缓冲、无缓冲
size_t size); //缓冲区大小
#include <stdio.h>
#include <unistd.h> //to use sleep() int main(void)
{
int i=; setvbuf( stdout, NULL , _IONBF , ); for(i=;i<;i++)
{
printf("%d ",i);
sleep(); //为了查看效果,才加上sleep();
} printf("\n");
return ;
}
程序执行的过程中: 可以看到数字一个一个的输出,而不是一起输出。
#include <stdio.h>
#include <unistd.h> //to use sleep() int main(void)
{
int i=;
char buf[]={}; /*setvbuf(stdout, NULL, _IONBF ,0);*/
setvbuf(stdout, buf , _IOLBF , ); for(i=;i<;i++)
{
printf("%d ",i);
sleep(); //为了查看效果,才加上sleep();
} printf("\n");
return ;
}
可以看到数组0、1、2、3、4是一个一个的输出,而不是一起输出。
#include <stdio.h>
#include <unistd.h> //to use sleep() int main(void)
{
int i=;
char buf[]={}; /*setvbuf(stdout, NULL, _IONBF ,0);*/
/*setvbuf(stdout, buf , _IOLBF , 1);*/ for(i=;i<;i++)
{
printf("%d ",i);
fflush(stdout);
sleep(); //为了查看效果,才加上sleep();
} printf("\n");
return ;
}
[root@localhost cpfile]# ./a.out main.c cpfile.c
(null) exist,do you want to overwrite it?(y/n):n
#include <stdio.h> int main(void)
{
char ch;
char ch_1;
char buf[];
char buf_1[]; scanf("%c%s",&ch,buf);
printf("c=%c, str=%s\n",ch,buf); scanf("%c%s",&ch_1,buf_1);
printf("c=%c, str=%s\n",ch_1,buf_1);
return ;
}
[root@localhost stdio]# vim scanf.c
[root@localhost stdio]# gcc scanf.c
[root@localhost stdio]# ./a.out
hello world //输入 hello world 然后按下回车键
c=h, str=ello
c= , str=world
#include <stdio.h> int main(void)
{
char ch;
char ch_1;
char buf[];
char buf_1[]; scanf("%c%s",&ch,buf);
printf("c=%c, str=%s\n",ch,buf); fflush(stdin);
scanf("%c%s",&ch_1,buf_1);
printf("c=%c, str=%s\n",ch_1,buf_1);
return ;
}
[root@localhost stdio]# ./a.out
hello wolrd
c=h, str=ello
c= , str=wolrd //输出结果为没有将输入数据缓冲区刷出
FOPEN() Linux Programmer’s Manual FOPEN()
NAME
fopen, fdopen, freopen - stream open functions SYNOPSIS
#include <stdio.h> FILE *fopen(const char *path, //要打开的文件
const char *mode); //打开模式 FILE *fdopen(int fildes, //已经用 open打开的文件的文件描述符
const char *mode); //打开模式,必须与open的模式兼容 // 下面的函数,将 stream 文件流重定向到 重新为 path 打开的文件流
FILE *freopen(const char *path,
const char *mode,
FILE *stream);
FCLOSE() Linux Programmer’s Manual FCLOSE()
NAME
fclose - close a stream
SYNOPSIS
#include <stdio.h> int fclose(FILE *fp);
FREAD() Linux Programmer’s Manual FREAD()
NAME
fread, fwrite - binary stream input/output
SYNOPSIS
#include <stdio.h> size_t fread(void *ptr, //存储读入数据的数据缓冲区首地址、指针
size_t size, //要读取的数据块的带小
size_t nmemb, //每次读取多少个数据块
FILE *stream); //要读取的文件流 size_t fwrite(const void *ptr, //存储待写入数据的数据缓冲区首地址、指针
size_t size, //要写入到数据块的大小
size_t nmemb, //每次要写入多少个数据块
FILE *stream); //要写入的文件流
FERROR() Linux Programmer’s Manual FERROR()
NAME
clearerr, feof, ferror, fileno - check and reset stream status
SYNOPSIS
#include <stdio.h> void clearerr(FILE *stream);
int feof(FILE *stream); //检测是否到文件尾
int ferror(FILE *stream);
int fileno(FILE *stream);
FSEEK() Linux Programmer’s Manual FSEEK()
NAME
fgetpos, fseek, fsetpos, ftell, rewind - reposition a stream
SYNOPSIS
#include <stdio.h> int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream);
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, fpos_t *pos);
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define BUF_SIZE 512 int main(int argc, char* argv[])
{
char buf[BUF_SIZE]={};
FILE* fp_src;
FILE* fp_dst; if(argc<)
{
printf("usage: cpfile file_src file_dst\n");
puts("\t file_src: the source file");
puts("\t file_dst: the target file");
exit();
} fp_src = fopen( argv[], "r");
fp_dst = fopen( argv[], "w"); while( !feof(fp_src) )
{
memset(buf, , sizeof(buf));
fread(buf, BUF_SIZE, , fp_src);
fwrite(buf, BUF_SIZE, , fp_dst);
} fclose(fp_src);
fclose(fp_dst);
return ;
}
执行的时候,可以成功复制文件。
【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口的更多相关文章
- 【linux草鞋应用编程系列】_2_ 环境变量和进程控制
一. 环境变量 应用程序在执行的时候,可能需要获取系统的环境变量,从而执行一些相应的操作. 在linux中有两种方法获取环境变量,分述如下. 1.通过main函数的参数获取环境变量 ...
- 【linux草鞋应用编程系列】_6_ 重定向和VT100编程
一.文件重定向 我们知道在linux shell 编程的时候,可以使用文件重定向功能,如下所示: [root@localhost pipe]# echo "hello world&q ...
- 【linux草鞋应用编程系列】_5_ Linux网络编程
一.网络通信简介 第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章. 二.linux网络通信 在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...
- 【linux草鞋应用编程系列】_4_ 应用程序多线程
一.应用程序多线程 当一个计算机上具有多个CPU核心的时候,每个CPU核心都可以执行代码,此时如果使用单线程,那么这个线程只能在一个 CPU上运行,那么其他的CPU核心就处于空闲状态,浪费了系 ...
- 【linux草鞋应用编程系列】_3_ 进程间通信
一.进程间通信 linux下面提供了多种进程间通信的方法, 管道.信号.信号量.消息队列.共享内存.套接字等.下面我们分别 介绍管道.信号量.消息队列.共享内存. 信号和套 ...
- 第3章 文件I/O(8)_贯穿案例:构建标准IO函数库
9. 贯穿案例:构建标准IO函数库 //mstdio.h #ifndef __MSTDIO_H__ #define __MSTDIO_H__ #include <unistd.h> #de ...
- linux标准io的copy
---恢复内容开始--- 1.linux标准io的copy #include<stdio.h> int main(int argc,char **argv) { if(argc<3) ...
- Linux C++ 网络编程学习系列(1)——端口复用实现
Linux C++ 网络编程学习系列(1)--端口复用实现 源码地址:https://github.com/whuwzp/linuxc/tree/master/portreuse 源码说明: serv ...
- linux makefile: c++ 编程_基础入门_如何开始?
学习android 终究还是需要研究一下其底层框架,所以,学习c++很有必要. 这篇博客,算是linux(ubuntu) 下学习 c++ 的一个入门. 刚开始学习编程语言的时候,最好还是使用命令行操作 ...
随机推荐
- 在MotionBuilder中绑定C3D动作和模型
[题外话] 实验室人手不足,虽然自己连MotionBuilder一点都没有用过,但是老板叫自己干也只能硬着头皮上了.本文详细介绍了MotionBuilder 2013中的摄像机操作以及在MotionB ...
- 使用C#给Linux写Shell脚本(下篇)
在上篇的<使用C#给Linux写Shell脚本>结尾中,我们留下了一个关于C#如何调用BashShell的问题.在文章发布之后,我留意到有读者留言推荐使用“Pash”(一款类PowerSh ...
- [ASP.NET MVC 小牛之路]07 - URL Routing
我们知道在ASP.NET Web Forms中,一个URL请求往往对应一个aspx页面,一个aspx页面就是一个物理文件,它包含对请求的处理. 而在ASP.NET MVC中,一个URL请求是由对应的一 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (9) -----第二章 实体数据建模基础之继承关系映射TPH
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-10 Table per Hierarchy Inheritance 建模 问题 ...
- Windows下安装python2和python3双版本
现在大家常用的桌面操作系统有:Windows.Mac OS.ubuntu,其中Mac OS 和 ubuntu上都会自带python.这里我们只介绍下Windows(我用的Win10)环境下的pytho ...
- Sublime文件夹显示过滤
Preferences/Setting-User添加如下命令: "file_exclude_patterns": ["*.mate", "*.gif& ...
- lua中的string类型
在lua中用union TString来表示字符串类型 lobject.h: 其中结构体tsv中 reserved字段表示字符串是不是保留关键字,hash是其哈希值,len是其长度.我们在TStrin ...
- 前端学PHP之面向对象系列第六篇——简单图形面积计算器实现
前面的话 本文用面向对象的技术来实现一个简单的图形面积计算器 图形类 //rect.class.php <?php abstract class Shape{ public $name; abs ...
- .NET平台机器学习组件-Infer.NET(三) Learner API—数据映射与序列化
所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html 微软Infer.NET机器学习组件:http://www.cnblo ...
- 构建自己的PHP框架--实现Model类(1)
在之前的博客中,我们定义了ORM的接口,以及决定了使用PDO去实现.最后我们提到会有一个Model类实现ModelInterface接口. 现在我们来实现这个接口,如下: <?php names ...