文件I/O编程

本文详细介绍了使用C语言进行文件操作,包括open(), read(), write(), fseek(), lseek()等函数的用法,以及标准C库如fopen(), fwrite(), fread(), fputc(), fgetc()的实战示例,涵盖了文件读写、结构体存取和不同权限模式。
摘要由CSDN通过智能技术生成

一. 打开文件

1. open函数

int fd = open("./test2",O_RDWR);

./表示文件与代码在同一路径下,否则需要写具体路径

fd:文件描述符,失败返回-1,成功返回大于0的值

O_RDWR:读写

O_RDONLY:只读

O_WRONLY:只写

O_APPEND:追加

O_CREAT:没有文件创建文件

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int fd;
	//读写打开文件
	fd = open("./test2",O_RDWR);
	if(fd==-1)
	{
		printf("open file failed\n");
		//没有文件创建文件,并以读写方式打开
		fd=open("./test2",O_RDWR|O_CREAT,0600);
		if(fd>0)
		{
			printf("create test2 succeed\n");
		}
	}
	if(fd>0)
	{
		printf("open test2 succeed\n");
	}
    return 0;
}

二. 读文件

1. read读函数

int n_read = read(fd,readBuf,size);

n_read:返回实际读出的字节数

fd:文件描述符

readBuf:缓冲区,存放读出的内容,一般为char *类型

size:要读多少字符

2. lseek函数

用于移动光标

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

fd:文件描述符

whence详解
whence属性效果
SEEK_SET光标移动到offset处
SEEK_END光标移动到程序最后+offset的位置,offset可正可负
SEEK_CUR光标留在当前位置+offset的位置,offset可正可负
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int fd;
	//读写打开文件
	fd = open("./test2",O_RDWR);
	if(fd>0)
	{
		printf("open test2 succeed\n");
		char *readBuf;
        //给缓冲区开辟空间
		readBuf = (char *)malloc(sizeof(char)*fd+1);
        //计算文件中内容字节数
		int size = lseek(fd,0,SEEK_END);
        //移动光标回到开始位置
		lseek(fd,0,SEEK_END);
        //读出内容到缓冲区
		int n_read = read(fd,readBuf,size);
		printf("read: %d,context: %s\n",n_read,readBuf);
		close(fd);
	}
    return 0;
}

三. 写文件

1. write函数

像文件中写入内容,但是会从文件初始位置开始写,并覆盖原位置的内容。

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int fd;
	//读写打开文件
	fd = open("./test2",O_RDWR);
	if(fd>0)
	{
		printf("open test2 succeed\n");
		char *buf="test message";
		int n_write = write(fd,buf,strlen(buf));
		if(n_write!=-1)
			printf("write success\n");
		close(fd);
	}
    return 0;
}

2. O_APPEND追加内容

会将内容追加到文件最后,不会覆盖原有内容

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int fd;
	//读写打开文件
	fd = open("./test2",O_RDWR|O_APPEND);
	if(fd>0)
	{
		printf("open test2 succeed\n");
		char *buf="test message";
		int n_write = write(fd,buf,strlen(buf));
		if(n_write!=-1)
			printf("write success\n");
		close(fd);
	}
    return 0;
}

3. O_TRUNC替换内容

会删去原来所有的内容,替换成新的

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int fd;
	//读写打开文件
	fd = open("./test2",O_RDWR|O_TRUNC);
	if(fd>0)
	{
		printf("open test2 succeed\n");
		char *buf="test message";
		int n_write = write(fd,buf,strlen(buf));
		if(n_write!=-1)
			printf("write success\n");
		close(fd);
	}
    return 0;
}

例:向文件中写入结构体

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
struct node{
	int a;
	char c;
};
int main()
{
	int fd;
	struct node data[2] = {{100,'a'},{200,'c'}};
	struct node data2[2];
	//读写打开文件
	fd = open("./test2",O_RDWR);
	if(fd>0)
	{
		printf("open test2 succeed\n");
		int n_write = write(fd,&data,sizeof(struct node)*2);
		lseek(fd,0,SEEK_SET);
		int n_read=read(fd,&data2,sizeof(struct node)*2);
		printf("read %d %c\n",data2[0].a,data2[0].c);
		printf("read %d %c\n",data2[1].a,data2[0].c);
		close(fd);
	}
    return 0;
}

例:向文件中写入整数

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int fd;
	int data=10,data2;
	//读写打开文件
	fd = open("./test2",O_RDWR);
	if(fd>0)
	{
		printf("open test2 succeed\n");
		int n_write = write(fd,&data,sizeof(struct node)*2);
		lseek(fd,0,SEEK_SET);
		int n_read=read(fd,&data2,sizeof(struct node)*2);
		printf("read %d\n",data2);
		close(fd);
	}
    return 0;
}

四. 标准C库

1. fopen函数

FILE *fp = fopen("test.txt","权限")

文件读写权限说明
打开方式说明
"r"只读,文件不存在会打开失败
"w"写入,文件不存在创建文件,原文件内容会被全部删除
"a"追加,文件不存在创建文件,写入的数据追加到文件末尾
"r+"读写,可读可写,文件不存在打开失败,写入的数据会从头开始覆盖相应长度的原内容
"w+"读写,可读可写,文件不存在创建文件,原文件内容会被全部删除
"a+"追加可读,文件不存在创建文件,写入的数据追加到文件末尾

2. fwrite函数/fread函数

size_t fwrite(const void *ptr,size_t size,size_t number,FILE *stream);

size_t fread(const void *ptr,size_t size,size_t number,FILE *stream);

将ptr(数组或字符指针)中的内容写到stream中

size:每次写入/读出的字节大小

number:写/读多少次

例: fwrite(str,sizeof(char)*strlen(str),1,fp); //一次性全部写入

        fwrite(str,sizeof(char),strlen(str),fp);//每次写一个字符

#include<stdio.h>
#include<string.h>
int main()
{
	FILE *fp;
	char *str="test message";
	char readBuf[128]={0};

	fp=fopen("./test2","w+");
	//fwrite(str,sizeof(char),strlen(str),fp);//每次写入一个字符
	fwrite(str,sizeof(char)*strlen(str),1,fp);//一次性全部写入
	fseek(fp,0,SEEK_SET);
	//fread(readBuf,sizeof(char),strlen(str),fp);//每次读出一个字符
	fread(readBuf,sizeof(char)*strlen(str),1,fp);//一次性读出所有字符
	printf("read data :%s\n",readBuf);
	fclose(fp);
    return 0;
}

例: 向文件中写入结构体

#include<stdio.h>
#include<string.h>
struct node{
	int a;
	char c;
};
int main()
{
	FILE *fp;
	struct node data[2]={{3,'q'},{5,'w'}};
	struct node data2[2];

	fp=fopen("./test2","w+");
	//fwrite(str,sizeof(char),strlen(str),fp);
	fwrite(&data,sizeof(struct node)*2,1,fp);
	fseek(fp,0,SEEK_SET);
	//fread(readBuf,sizeof(char),strlen(str),fp);
	fread(&data2,sizeof(struct node)*2,1,fp);
	printf("read data :\n%d  %c\n",data2[0].a,data2[0].c);
	printf("%d  %c\n",data2[1].a,data[1].c);
	fclose(fp);
    return 0;
}

3. fputc,fgetc,feof

fputc:写入一个字符

fgetc:输出一个字符

feof:查看是否到达文件末尾

#include<stdio.h>
#include<string.h>
int main()
{
	FILE *fp;
	char *str = "test message";
	int i;
	int len = strlen(str);

	fp = fopen("./test2","w+");
	for(i=0;i<len;i++)
	{
		fputc(*str,fp);
		str++;
	}

	fclose(fp);
	fp = fopen("./test2","r+");
	while(!feof(fp))
	{
		printf("%c",fgetc(fp));
	}
	printf("\n");
	fclose(fp);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值