工程实训(一)

嵌入式工程实训每日小结(一)

关于嵌入式的工程实训开始了,首先对于嵌入式要有一个笼统的概论。嵌入式开发就是指在嵌入式操作系统下进行开发,“一切皆文件”在linux中对目录的使用等同于文件的使用,文件分为:普通文件,目录文件,链接文件,设备文件。
第一天的学习内容总结:

1.开始学习点概念,文件及文件描述符。

  • 当打开一个现存文件或创建一个新文件时,内核就向进程返回一个文件描述符;当需要读写文件时,也需要把文件描述符作为参数传递给相应的函数。
  • 文件描述符是一个非负的整数,它是一个索引值,并指向在内核中每个进程打开文件的记录表。
  • 一个进程启动时,都会打开3个文件:标准输入、标准输出和标准出错处理。
    2.creat的应用。先熟悉使用man 2 creat ,linux会显示creat的所有使用方法及方式。在creat(创建)中,用上说明中的格式,加上它所要有的头文件。多应用说明中的格式(形参类型)。
    附参考程序:
    #include <stdio.h>
    #include <sys/types.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.open的应用。open有两种应用,一种是打开已经创建好了的文件,这时候只需要两个形参:文件名,权限。如果是打开一个没有创建的文件,就需要在程序中设置三个形参,它会先创建好文件,再进行打开文件的操作。man 2 open是尽快对于open的上手说明。
附参考程序:
#include <stdio.h>
#include <sys/types.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;

}
4.open(打开)之后就应该进行write(写)了。依然是使用man 2 write进行使用说明。write(写)还是先要打开文件,再把要写的内容写入一个中转函数中,在赋给所要写入的文件中。
附参考程序:
#include <stdio.h>
#include <sys/types.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.read的使用。write(写)之后就是read(读)了。read(读)跟write的用法差不多,也是先打开文件,再将文件中的内容读出来,如果要显示出来,要加上printf函数。
附参考程序:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
int fd, ret;
char buf[32] = {0}; //存放读取的数据

//只读的方式打开文件,文件必须存在
fd = open("hello.txt", O_RDONLY);
if (-1 == fd)
{
	perror("open");
	exit(1);
}

ret = read(fd, buf, sizeof(buf));
if (-1 == ret)
{
	perror("read");
	exit(1);
}

printf("read from txt : %s\n", buf);

close(fd);   //关闭文件
return 0;

}
6.第一天学习内容最后的一个要点。copy的使用,copy(复制)是将任何一个文件拷贝要其他的地方(可重命名)。copy的使用上要比之前的几个要复杂一点。copy的本质是将一个文件打开读取里面的内容,再在另一个目录下创建一个文件并将内容送入其文件中。就像是read(读)与write(写)的综合应用。注意在写入文件时,数据大小的使用,是有多少写多少。
附参考函数:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.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;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值