嵌入式第十七天!(文件IO)

文件IO:

标准IO和文件IO的区别:

    1. 标准IO是库函数,是对系统调用的封装

    2. 文件IO是系统调用,是Linux内核中的函数接口

    3. 标准IO是有缓存的

    4. 文件IO是没有缓存的

  1. 操作步骤:

        打开 -> 读/写 -> 关闭

  2. 打开文件(open):

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

    功能:打开文件并且获得文件描述符

    参数:

        pathname:要打开的文件名

        flags:标志位

O_RDONLY只读
O_WRONLY只写
O_RDWR读写
O_APPEND追加
O_ASYNC异步IO
O_CREAT文件不存在创建
O_TRUNC文件存在截断(清0)

        返回值:

            成功返回文件描述符(很小的非负整数)

            失败返回-1

        新生成的文件描述符总是为尚未使用的最小的非负整数

        0:stdin        1:stdout        2:stderr

  3. 关闭文件(close):

int close(int fd);

    功能:将fd对应的文件描述符关闭

  4. 读写(read/write)

    1. write

ssize_t write(int fd, const void *buf, size_t count);

    功能:向fd对应的文件中写入buf指向的count个字节

    参数:

        fd:文件描述符

        buf:写入数据空间首地址

        count:写入的字节数

    返回值:

        成功返回实际写入字节数

        失败返回-1

    2. read

ssize_t read(int fd, void *buf, size_t count);

    功能:从文件描述符fd对应的文件中读取count个字节存放到buf开始的空间中

    参数:

        fd:文件描述符

        buf:存放数据空间的首地址

        count:想要读取数据字节数

    返回值:

        成功返回实际读到的字节数

        失败返回-1

        读到文件末尾返回0

  5. lseek:

off_t lseek(int fd, off_t offset, int whence);

    功能:重新设定文件描述符的偏移量

    参数:

        fd:文件描述符

        offset:偏移量

        whence:

            SEEK_SET:文件开头

            SEEK_CUR:文件当前位置

            SEEK_END:文件末尾

    返回值:

        成功返回当前偏移量

        失败返回-1 

作业:

        1. 利用read和write实现文件内容的拷贝(将src.jpg中的内容拷贝到dst.jpg文件中)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
	int fsrc = 0;
	int fdst = 0;
	char tmpbuff[4096] = {0};
	ssize_t nret = 0;
	
	fsrc = open("src.jpg", O_RDONLY);
	if(fsrc == -1)
	{
		perror("fail to open");
		return -1;
	}

	fdst = open("dst.jpg", O_WRONLY | O_TRUNC | O_CREAT, 0664);
	if(fdst == -1)
	{
		perror("fail to open");
		return -1;
	}

	while(1)
	{
		nret = read(fsrc, tmpbuff, sizeof(tmpbuff));
		if(nret <= 0)
		{
			break;
		}
		write(fdst, tmpbuff, nret);
	}

	close(fsrc);
	close(fdst);

	return 0;
}

        2.1028:人口普查

#include <stdio.h>
#include <string.h>

typedef struct peopele
{
	char name[8];
	char birthday[12];
}peopele_t;

int main(void)
{
	peopele_t a[100000];
	int n = 0;
	int i = 0;
	int cnt = 0;
	char maxvalue[12] = {"2014/09/06"};
	char minvalue[12] = {"1814/09/06"};
	int curmax = 0;
	int curmin = 0;

	scanf("%d", &n);

	for(i = 0; i < n; i++)
	{
		scanf("%s%s", a[i].name, a[i].birthday);
	}

	for(i = 0; i < n; i++)
	{
		if(strcmp(a[i].birthday, maxvalue) <= 0 && strcmp(a[i].birthday, minvalue) >= 0)
		{
			cnt++;

			if(cnt == 1)
			{
				curmax = curmin = i;
			}

			if(strcmp(a[i].birthday, a[curmax].birthday) > 0)
			{
				curmax = i;
			}
			if(strcmp(a[i].birthday, a[curmin].birthday) < 0)
			{
				curmin = i;
			}
		}
	}

	printf("%d %s %s\n", cnt, a[curmin].name, a[curmax].name);

	return 0;

}

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值