linux下系统编程之文件i/o

1-open.c

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

int main()
{
	int fd = open("hello.txt", O_RDWR);  //以读写的方式打开文件hello.txt
	if (-1 == fd)          //返回-1表示出错 出错的原因保存在errno里面
	{
		//printf("%d\n", errno);
		perror("open");    //打印错误信息
		exit(1);           //退出程序
	}
	//printf("%d\n", fd);

	close(fd);

	fd = open("hello.c", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);   //创建文件,并且以读写方式打开文件
	if (-1 == fd)
	{
		perror("open1");
		exit(2);
	}

	char *s = "helloworld";
	ssize_t ret = write(fd, s, strlen(s));
	if (-1 == ret)
	{
		perror("write");
		exit(1);
	}

	//lseek(fd, 0, SEEK_SET);    //相对文件开头移动0个字节
	//lseek(fd, -10, SEEK_END);    //相对文件末尾向前移动10个字节
	lseek(fd, -10, SEEK_CUR);    //相对当前位置向前移动10个字节

	char buf[128] = {0};
	ret = read(fd, buf, sizeof(buf));
	if (-1 == ret)
	{
		perror("read");
		exit(1);
	}
	printf("read from text : %s\n", buf);

	close(fd);

	return 0;
}

2-lseek.c

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

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		return -1;
	}

	int fd = open(argv[1], O_RDWR);
	if (-1 == fd)
	{
		perror("open");
		exit(1);
	}

	int ret = lseek(fd, 0, SEEK_END);
	printf("%d\n", ret);

	close(fd);

	return 0;
}

3-copy.c

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

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		printf("error\n");
		exit(1);
	}

	int fd_from = open(argv[1], O_RDONLY);
	if (-1 == fd_from)
	{
		perror("open1");
		exit(2);
	}

	int fd_to = open(argv[2], O_WRONLY | O_CREAT, 00700);
	if (-1 == fd_to)
	{
		perror("open2");
		exit(3);
	}

	char buf[128] = {0};
	ssize_t ret;
	while (1)
	{	
		ret = read(fd_from, buf, sizeof(buf) - 1);
		if (-1 == ret)
		{
			perror("read");
		}
		else if (0 == ret)
		{
			printf("拷贝完毕\n");
			break;
		}

		ret = write(fd_to, buf, ret);
		if (-1 == ret)
		{
			perror("write");
		}
	}

	close(fd_from);
	close(fd_to);

	return 0;
}

4-fopen.c

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

int main()
{
	//FILE *fp = fopen("hello.c", "r");       //只读方式打开文件
	FILE *fp = fopen("hello.c", "w+");    //读写方式打开,如果存在,清空文件,如果不存在,创建文件
	if (NULL == fp)
	{
		perror("fopen");
		exit(1);
	}

	char *s = "this is test ...";
	size_t ret = fwrite(s, 1, strlen(s), fp);
	if (0 == ret)
	{
		perror("fwrite");
		exit(2);
	}
	
	fseek(fp, 0, SEEK_SET);   //成功返回0

	char buf[32] = {0};
	ret = fread(buf, 1, sizeof(buf), fp);
	if (0 == ret)
	{
		perror("fread");
		exit(2);
	}
	printf("%s\n", buf);

	fclose(fp);

	return 0;
}

5-copy.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		printf("error\n");
		exit(1);
	}

	FILE *fp_from = fopen(argv[1], "r");    //只读方式打开源文件
	if (NULL == fp_from)
	{
		perror("fopen");
		exit(2);
	}

	FILE *fp_to = fopen(argv[2], "w");     //只写方式创建并打开目的文件
	if (NULL == fp_to)
	{
		perror("fopen2");
		exit(3);
	}

	char buf[128] = {0};
	size_t ret;

	while (1)
	{	
		ret = fread(buf, 1, sizeof(buf) - 1, fp_from);
		if (0 == ret)
		{
			printf("拷贝完毕\n");
			break;
		}

		ret = fwrite(buf, 1, ret, fp_to);
		if (0 == ret)
		{	
			perror("fwrite");
		}
	}

	fclose(fp_from);
	fclose(fp_to);

	return 0;
}

6-结构体.c

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

struct Node 
{
	int data;
	char ch;
};
typedef struct Node Node;

int main()
{
	Node n = {1, 'a'};

	int fd = open("hello.txt", O_WRONLY | O_CREAT, 00700);

	write(fd, &n, sizeof(Node));

	close(fd);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值