文件读写函数的应用

1.函数说明

read()函数从文件中读取数据存放到缓冲区中,并返回实际读取的字节数。若返回0,则表示没有数据可读,即已到达文件尾。读操作从文件当前读写位置开始读取内容,当前读写位置自动往后移动。

write()函数将数据写入文件中,并返回实际写入的字节数。写操作从文件的当前读写位置开始写入。对磁盘文件进行写操作时,若磁盘已满,write()函数返回失败。

2.函数格式

read()

头文件#include<unistd.h>
函数原型ssize_t read (int fd, void *buf,size_t count)
函数传入值

fd:文件描述符

buf:指定存储器读出数据的缓冲区

count:指定读出的字节数

返回值

成功:读到的字节数

0:已到达文件末尾

-1:出错

write()

头文件#include<unistd.h>
函数ssize_t read (int fd, void *buf,size_t count)
传入值

fd:文件描述符

buf:指定写入数据的缓冲区

count:指定读出的字节数

返回值

成功:以写入的字符

-1:出错

read()代码案例

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

typedef struct student
{
	char name[20]
		int year;
}Stu;

int main(int argc, char *argv[])
{
	Stu stu = {0};
	int st = open(argv[1], O_RDONLY);
	if(0 > st)
	{
		perror("open error");
		return -1;
	}
	printf("open ok!\n");

	int ret = read(st, stu.name, sizeof(stu.name));
	if(ret < 0)
	{
		perror("read error");
		close(st);
		return -1;
	}
	int ret1 = read(st,&stu.year, sizeof(stu.year));
	if(ret < 0)
	{
		perror("read error");
		close(st);
		return -1;
	}
	printf("%s\t",stu.name); 
	printf("%d\n",stu.year);
	printf("write ok!\n");

	close(st);

	return 0;
}

write()案例

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

#define SIZE 2
#define N 3
typedef struct student
{
	char name[20];
	float sorces[N];
}Stu;

void swap(Stu *pa, Stu *pb);
void swap(Stu *pa, Stu *pb)
{
	Stu p = {0};
	p = *pa;
	*pa = *pb;
	*pb = p;
}
void input(Stu *p, int count);
void input(Stu *p, int count)
{
	int i, j;
	for(i = 0; i < count; i++)
	{
		printf("请输入学生信息\n");
		scanf("%s", p-name);
		for(j = 0; j < N; j++)
		{
			scanf("%f",&p->sorces[j]);
		}
		p++;
		printf("\n");
	}
}
void output(Stu *p, int count);
void output(Stu *p, int count)
{
	int i, j;
	for(i = 0; i < count; i++)
	{
		printf("name:%s\n",p->name);
		printf("sorces:");
		for(j = 0; j < N; j++)
		{
			printf("%8.1f",p->sorces[j]);
		}
		p++;
		printf("\n");
	}
}
float sum(Stu *p, int temp);
float sum(Stu *p, int temp)
{
	float sum = 0;
	int i;
	for(i = 0; i < temp; i++)
	{
		sum += p->sorces[i];
	}
	return sum;
}

void sortBySorces(Stu *p, int count);
void sortBySorces(Stu *p, int count)
{
	int i,j;
	for(i = 0; i < count-1; i++)
	{
		for(j = 0; j < count-i-1; j++)
		{
			if(sum((p+j),N) < sum((p+j+1),N))
			{
				swap((p+j),(p+j+1));
			}
		}
	}
}
int main(int argc, char *argv[])
{
	Stu *p = (Stu *)malloc(SIZE *sizeof(Stu));
	memset(p,0,SIZE*sizeof(Stu));
	int st = open(argv[1],O_WRONLY);
	if(0 > st)
	{
		input(p,SIZE);
		sortBySorces(p,SIZE);
		int st = open(argv[1],O_WRONLY|O_CREAT,0666);
		if(st < 0)
		{
			printf("open_write error!\n");
		}
		int ret = write(st,p,SIZE *sizeof(Stu));
		if(0 > ret)
		{
			perror("error");
			close(st);
			p = NULL;
			return -1;
		}
	}
	else 
	{
		int ret = read(std,p,SIZE*sizeof(Stu));
		if(ret < 0)
		{
			perror("read error");
			close(st);
			p = NULL;
			return -1;
		}
		output(p,SIZE);
	}
	close(st);
	free(p);
	p = NULL; 
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值