第6章应用编程-课后作业

1.centos 7中,安装gcc、c++编译器以及内核文件

yum -y install gcc gcc-c++ kernel-devel

2.测试gcc环境和hello world

 1 gcc -v 2 arm-linux-gcc -v 

1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 int main(){
5   printf("Hello World!\n");
6   exit(0);
7 }
1 //生成a.out文件
2 gcc hello_world.c
3 //生成hello文件
4 gcc -o  hello_world hello_world.c
5 //执行
6 ./a.out

3.应用编程open,write,read,lseek,close

b站视频:https://www.bilibili.com/video/BV1VW411m7kw

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>//1~3 open引用的库文件
#include<stdio.h>
#include<unistd.h>//write read引用的库文件
#include <sys/types.h>//lseek引用的库文件 man 2 lseek
#include <unistd.h>


int main(void)
{
//判断find2.txt是否存在,存在打开find2.txt,不存在新建一个
int fd = 0;
fd=open("./find3.txt",O_RDWR);
if(fd == -1){  
printf("open  failed\n");
fd=open("./find3.txt",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("creat find3.txt success!\n");
}
}
//把hello world写入find2.txt,光标到末尾
char buf1[] = "hello world";
write(fd,(void *)buf1,11);

//调节光标到字符起始位置
lseek(fd,SEEK_SET,0);

//从起始光标0位置开始读取fd文件内容,并输出buf2里面
char buf2[30] = {0};
read(fd,buf2,sizeof(buf2));
printf("buf2 = %s\n",buf2);

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

int main(void)
{
    int fd;
    close(STDOUT_FILENO);//看不懂,标准输出的文件描述子为 1 (一)。POSIX <unistd.h> 定义是 STDOUT_FILENO
    fd = open("cat", O_CREAT | O_RDWR, 0664);//创建cat文件可读可写
    if (fd < 0) {
        perror("open");
        exit(-1);
    }
    printf("hello world\n");
    return 0;
}

  4.用到的基础代码

//linux 帮助手册
#man 2 lseek
//将目前目录及其子目录下所有延伸档名是 c 的文件列出来。
# find . -name "*.c"
#cd /usr/linux-2.6.32.2/1
# find / |grep syscall_table 
//通过strace命令可以查看操作系统命令所调用的系统调用
#strace ls
//复制粘贴
全部删除:按esc键后,先按gg(到达顶部),然后dG
全部复制:按esc键后,先按gg,然后ggyG
全选高亮显示:按esc键后,先按gg,然后ggvG或者ggVG

单行复制:按esc键后, 然后yy
单行删除:按esc键后, 然后dd
粘贴:按esc键后, 然后p
复制到粘贴板: 全选高亮显示之后,ctrl+shift+c,

  

posted @ 2020-05-17 18:07  凌易说-lingyisay  阅读(176)  评论(0编辑  收藏  举报