1.要求创建一个time.txt,存储内容格式如下:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("time.txt","a+");
int num=1,a;
while((a=fgetc(fp))!=-1){ // 统计之前的序号
if(a==10){
num++;
}
}
while(1){
size_t t=time(NULL);
struct tm *ct=localtime(&t);
fprintf(fp,"[%d] %02d-%02d-%02d %02d:%02d:%02d\n",
num,\
ct->tm_year+1900,\
ct->tm_mon+1,\
ct->tm_mday,\
ct->tm_hour,\
ct->tm_min,\
ct->tm_sec);
fflush(fp); // 刷新缓冲区,式字符串即时输入到文件
num++;
sleep(1);
}
fclose(fp);
return 0;
}
2.用read和write拷贝一张图片
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int fo = open(argv[1],O_RDONLY);
char *p = argv[1];
while(*p++!='.');
char cpfile[20]="cpy.";
int i=4;
while(*p){
cpfile[i]=*p;
i++;
p++;
}
int fc = open(cpfile,O_WRONLY|O_CREAT|O_TRUNC,0664);
char buf[20];
while(i=read(fo,buf,20)){
write(fc,buf,i);
}
close(fo);
close(fc);
return 0;
}