七十一. makefile练习
题目:
问题1的解答:
七十二. read , write函数写文件
read_write.c内容
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
int fd = open("english.txt", O_RDONLY);
if(fd == -1)
{
perror("open error");
exit(1);
}
// 读文件
char buf[1024];
int len = read(fd, buf, sizeof(buf));
// 写文件
int fd1 = open("temp", O_WRONLY | O_CREAT, 0664);
if(fd1 == -1)
{
perror("open error");
exit(1);
}
while( len > 0 )
{
// 写文件
int ret = write(fd1, buf, len);
len = read(fd, buf, sizeof(buf));
}
close(fd);
close(fd1);
return 0;
}
七十三. perror和errno
七十四. lseek实现文件拓展
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
int main(int argc, const char* argv[])
{
int fd = open("copy", O_RDWR);
if(fd == -1)
{
perror("open error");
exit(1);
}
// 拓展文件
int len = lseek(fd, 0, SEEK_END);
printf(" file len = %d\n", len);
lseek(fd, 100000, SEEK_END);
write(fd, "a", 1);
close(fd);
return 0;
}
七十五. 阻塞读终端
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
// ▒▒▒▒▒▒ն▒
int main(void)
{
char buf[10];
int n;
n = read(STDIN_FILENO, buf, 10);
if (n < 0)
{
perror("read STDIN_FILENO");
exit(1);
}
write(STDOUT_FILENO, buf, n);
return 0;
}
[root@VM_0_15_centos Block]# ls
block block_read.c time timeout_unblock_read.c unblock unblock_read.c
[root@VM_0_15_centos Block]# gcc block_read.c -o block_app
[root@VM_0_15_centos Block]# ls
block block_read.c timeout_unblock_read.c unblock_read.c
block_app time unblock
[root@VM_0_15_centos Block]# ./block_app
hello,worldabs
hello,worl[root@VM_0_15_centos Block]# dabs
-bash: dabs: command not found