苏嵌实训一
日期:2018年10月8日
班级:15电信单招班
姓名:葛丽丽
学号:15090733
本人进度计划以及任务:学习C语言相关知识。
本日任务完成情况:完成了嵌入式开发环境的搭建。
学习了c语言相关知识。
本日开发中出现的问题汇总:本人觉得自己在c语言方面还有所欠缺,掌握不够牢固,还需加强对部分知识点的理解和把握。希望在以后的学习生活中可以有所提高。
本日未解决的问题:个别知识点没有能够理解,导致学习进度没有跟上。
本日开发收获:学习了c语言的相关知识,并编写了一些程序。
自我评价:基本完成任务,课后仍需努力。
(1)
#include <stdio.h>
#include <sysypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
int fd;
//以读写方式打开hello.txt 文件必须存在
/*fd = open("hello.txt", O_RDWR);
if (-1 == fd) //打开失败,返回-1
{
perror("open"); //失败打印错误信息
exit(1);
}
close(fd);*/ //关闭文件
//文件不存在,先创建文件再打开,如果文件存在,报错
fd = open("aaa", O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
if (-1 == fd)
{
perror("open");
exit(1);
}
close(fd);
return 0;
}
(2)
#include <stdio.h>
#include <sysypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
int main()
{
int fd; //file descriptor文件描述符
//创建名为hello.txt的文件
fd = creat("hello.txt", S_IRWXU | S_IRWXG);
if (-1 == fd) //返回值判断
{
printf("%d\n", errno); //保存错误码
perror("aaaaaa"); //打印错误
exit(100); //退出程序
}
return 0;
}
(3)
#include <stdio.h>
#include <stdlib.h>
#include <sysypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fd_from, fd_to, ret;
if (argc != 3)
{
printf("error\n");
exit(1);
}
//打开源文件
fd_from = open(argv[1], O_RDONLY);
if (-1 == fd_from)
{
perror("open");
exit(1);
}
//打开目的文件
fd_to = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 00700);
if (-1 == fd_to)
{
perror("open");
exit(1);
}
char buf[32] = {0};
while ((ret = read(fd_from, buf, 31)) != 0)
{
ret = write(fd_to, buf, ret);
if (-1 == ret)
{
perror("write");
break; //结束循环
}
memset(buf, 0, 32); //清空buf
}
return 0;
}
(4)
#include <stdio.h>
#include <sysypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd, ret;
char buf[32] = "helloworld1234";
//打开(创建)文件
fd = open("hello.txt", O_WRONLY | O_CREAT, 00700);
if (-1 == fd) //如果打开失败
{
perror("open");
exit(1);
}
ret = write(fd, buf, strlen(buf));
if (-1 == ret) //返回-1,失败
{
perror("write");
exit(1);
}
close(fd); //关闭文件
return 0;
}
(5)
#include <stdio.h>
#include <sysypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd, ret;
char buf[32] = "helloworld1234";
//打开(创建)文件
fd = open("hello.txt", O_WRONLY | O_CREAT, 00700);
if (-1 == fd) //如果打开失败
{
perror("open");
exit(1);
}
ret = write(fd, buf, strlen(buf));
if (-1 == ret) //返回-1,失败
{
perror("write");
exit(1);
}
close(fd); //关闭文件
return 0;
}
(6)
#include <stdio.h>
#include <stdlib.h>
#include <sysypes.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main()
{
int fd, ret;
char buf1[32] = "1234567890";
char buf2[32] = {0};
//读写方式打开(创建)文件
fd = open("hello.txt", O_RDWR | O_CREAT, 00700);
if (-1 == fd)
{
perror("open");
exit(1);
}
ret = write(fd, buf1, strlen(buf1));
if (-1 == ret)
{
perror("write");
exit(1);
}
lseek(fd, 0, SEEK_SET); //文件指针移动到文件开头
ret = read(fd, buf2, sizeof(buf2));
if (-1 == ret)
{
perror("read");
exit(1);
}
printf("%s\n", buf2);
close(fd);
return 0;
}