全部手写不知道能写多少看心情
第一个
Linux代码编译出来要./执行(我傻逼了好久)
#include <unistd.h> //当前环境的头文件
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h> // 关于目录的操作文件
int main (int argc,char *argv[])//argc 是参数个数 *argv[]根据右到左的优先级得出二级指针
{
DIR *dp; //一结构体存了目录的信息
struct dirent *dirp;//存关于文件的信息
if(argc!= 2)
perror("a single argument (the directory name ) is required");
if((dp= opendir(argv[1]))==NULL)
perror("can't open file");
while((dirp=readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
closedir(dp); //打开就要关闭
exit(0);
}
第二个 不用缓存的I/O open write read lseek
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#define BUFSIZE 8192
int main (void)
{
int n;
char buf[BUFSIZE];
while((n=read(STDIN_FILENO,buf,BUFSIZE))>0)//大写的是unistd.h经过定义的标准输入流
if(write(STDOUT_FILENO,buf,n)!=n)
perror("write err");
if(n<0)
perror("read err");
exit(0);
}
第三个 getc只能读一个字符
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int c;
while((c=getc(stdin))!=EOF) //书上没有加大括号导致我编译器会报警告
if(putc(c,stdout)==EOF) //向标准输出流扔一个字符
perror("output error");
if(ferror(stdin))
perror("input error");
perror("das"); //当我再键盘敲出EOF事输出就是这个这也就是perror用法 das: Success
exit(0);
}
明天继续