文件编译初学习

这两天博客都没则么更新,倒不是因为我懒了,实在是没什么拿得出手的。对我来说,文件编译这几个函数都是新知,所以所写与所学都是些初步理解。这些东西,知道了也就只是了解了,也就是些简单的试用,咋么好意思发出去呢。

正真的学习,是要在实践中一步一步探索的,绝不是我这样寥寥一试。但是我也没有办法,也许是我学的慢吧,这几天我就看了这么多。罢了,我写博客的初衷是为了记录我的学习过程,也就没那多不好意思了。


系统调用

open

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

int main()
{
	//关闭标准输出
	close(1);
	
	int fd = open("哎呦不错",O_WRONLY | O_CREAT,0777);
	//int ph = open("哎呦不错",O_RDONLY );
	if(fd == -1)
	{
		printf("文件创建失败\n");
		perror("open");
		printf("%s\n",strerror(errno));
	}
	
	printf("fd = %d\n",fd);
	printf ("asdadasdad\n");
	printf ("asdadasdad\n");
	printf ("asdadasdad\n");
	printf ("asdadasdad\n");
	printf ("asdadasdad\n");
	printf ("asdadasdad\n");
	printf ("asdadasdad\n");

	//close (fd);   
	//不能关闭  关闭了就存不进去
	return 0;
}

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

#define SIZE 1024

int main1()
{
	int fd = open("BTree.c",O_RDWR);
	if (fd == -1)
	{
		perror("open");
		return -1;
	}
	
	char buf[SIZE] = {0};
	ssize_t ret = read(fd,buf,SIZE-1);
	if(ret == -1)
	{
		perror("open");
		return -1;
	}
	
	if(ret == 0)
	{
		printf("文件读取结束。\n");
	}
	
	printf("ret = %d\n",strlen(buf));
	printf("读到 %d 字节: %s\n",ret,buf);
	
	return 0;
	
}

//缓冲区覆盖问题
//若不每次读取晚对缓冲区进行清零
//会引起在最后的二次读完之后,后面的没有被覆盖
//清空缓冲区可以用menset函数
int main2()
{
	int fd = open("BTree.c",O_RDWR);
	if (fd == -1)
	{
		perror("open");
		return -1;
	}
	
	char buf[SIZE] = {0};
	
	while(1)
	{
		ssize_t ret = read(fd,buf,SIZE-1);
		
		if(ret == -1)
		{
			perror("open");
			return -1;
		}
		
		if(ret == 0)
		{
			printf("文件读取结束。\n");
			break;
		}
		printf("%s",buf);
		memset(buf,0,SIZE);  //清空缓冲区
	}
	return 0;
}
//或者读取完在后面加'\0'
int main3()
{
	int fd = open("BTree.c",O_RDWR);
	if (fd == -1)
	{
		perror("open");
		return -1;
	}
	
	char buf[SIZE] = {0};
	
	while(1)
	{
		ssize_t ret = read(fd,buf,SIZE-1);
		
		if(ret == -1)
		{
			perror("open");
			return -1;
		}
		
		if(ret == 0)
		{
			printf("文件读取结束。\n");
			break;
		}
		buf[ret] = '\0';  //读取热天个字节,buf[ret]是读取完后面一个
		printf("%s",buf);
	}
	return 0;
}

//读完一个大数据
//有些时候,为了方式读取被打断,
int main()
{
	int fd = open("BTree.c",O_RDWR);
	if (fd == -1)
	{
		perror("open");
		return -1;
	}
	
	char buf[SIZE] = {0};
	ssize_t ret = 0;
	int count = SIZE-1;  // 每一次要读的数据个数
	char *p = buf;
	while(ret = read(fd,p,count))
	{
		if(ret == -1)
		{
			//当没有数据可以读和读取数据被信号打断的时候,跳过
			if(errno == EAGAIN || errno == EINTR)
			{
				continue;  //
			}
			break;
		}
		
		
		//读完
		if(count == ret)
		{
			break;
		}
		count = count-ret;
		p += ret;
	}
	
	//这只能保证你一次读到你要的字节,不能保证你把文件读完
	return 0;
}
write

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


#define SIZE 1024

int main()
{
	int fd = open("xie",O_RDWR | O_CREAT ,0777);
	if (fd == -1)
	{
		perror("open");
		return -1;
	}
	
	char buf[SIZE] = {0};
	ssize_t ret = 0;
	while(1)
	{
		fgets(buf,SIZE,stdin);
		if(strncmp("end",buf,3)==0)
			break;
		ret = write(fd,buf,strlen(buf));
		if(ret == -1)
		{
			perror("write");
			break;
		}
		
		printf("要写入的字节数: %d, 实际写入的字节数:%d\n",SIZE,ret);
	}

	return 0;
	
}
lseek
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
	//打开文件
	int fd = open("abc2", O_RDWR | O_CREAT,0777);
	if(fd == -1)
	{
		perror("open");
		return -1;
	}
	
	lseek(fd,20,SEEK_SET);
	char buf[]  = "hello";
	
	write(fd,buf,strlen(buf));
	close(fd);
	
	return 0;
}

//使用lseek可以制作大文件,只要把文件偏移度设的很大,然后随便写入点什么。
试用系统调用实现简单的复制

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


#define SIZE 1024

int main()
{
	int fd1 = open("1.pptx",O_RDONLY);
	if (fd1 == -1)
	{
		perror("open fd1");
		return -1;
	}
	int fd2 = open("2.pptx",O_WRONLY | O_CREAT,0777);
	if (fd2 == -1)
	{
		perror("open fd2");
		return -1;
	}
	
	int ret ;
	char buf[SIZE] = {0};
	while(ret = read(fd1,buf,SIZE))
	{
		if (ret == -1)
		{
			perror ("read");
			break;
		}
		
		write(fd2,buf,ret);
	}
	
	close(fd1);
	close(fd2);
	return 0;
}


然后是标准 I/O库

fopen

#include<stdio.h>

int main()
{
	FILE *pf = fopen("abc","w+");
	if(pf == NULL)
	{
		printf("文件打开失败。\n");
		perror("fopen");
	}
	else
		printf("文件打开成功.\n");

	return 0;
}
fread

#include<stdio.h>

#define SIZE 1024

int main()
{
	FILE *fp = fopen("BTree.c","r+");
	if(fp == NULL)
	{
		perror ("fopen");
		return -1;
	}
	
	char buf[SIZE];
	int ret;
	//这是循环读取,读到文件结束
	while(ret = fread(buf,sizeof(char),SIZE-1,fp))
	{
		buf[ret*sizeof(char)] = '\0';   //读取完毕后至零
		printf("%s",buf);
	}
	
	//因为ret不管是文件错误还是读取完毕都返回0
	//所以要考虑feof()函数,
	//读取完毕返回非0
	if(ret == 0 && !feof(fp))
	{
		perror("pread");
		return -1;
	}
  	
	printf("文件读取结束\n");
	return 0;
}
调用标准I/O实现的复制

fread fwrite 版

#include<stdio.h>
#define SIZE 1024

int main()
{
	FILE *fp1 = fopen("1.pptx","r+");    //只读
	if(fp1 == NULL)
	{
		printf("文件打开失败。\n");     
		perror("fopen fp1");
		return -1;
	}
	FILE *fp2 = fopen("2.pptx","w+");    //只写(没有创建) 打开清零
	if(fp2 == NULL)
	{
		printf("文件打开失败。\n");
		perror("fopen fp2");
		return -1;
	}
	
	char buf[SIZE] = {0};
	size_t ret;
	while(fread(buf,sizeof(char),SIZE,fp1))
	{
		fwrite(buf,sizeof(char),SIZE,fp2);
	}
	
	if(ret == 0 && !feof(fp1))
	{
		perror("fread");
		return -1;
	}
	
	fclose(fp1);
	fclose(fp2);

	return 0;
}
fgetc fputc 版
#include<stdio.h>

#define SIZE 1024

int main()
{
	FILE *fp = fopen("1.pptx", "ab+");
	if (fp == NULL)
	{
		perror ("fopen");
		return -1;
	}
	
	FILE *fp1 = fopen("2.pptx", "ab+");
	if (fp1 == NULL)
	{
		perror ("fopen");
		return -1;
	}
	
	while(1)
	{
		int c = fgetc(fp);
		if(c == EOF)
			break;
		fputc(c,fp1);
	}
	fclose(fp);
	fclose(fp1);
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值