苏嵌实训

日期:2018年10月8日
班级:15电信单招班
姓名:彭嘉馨
学号:15090736

本人进度计划以及任务:学习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;

}
~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值