I/O:标准io

一、标准IO的操作函数

1.通过文件名,管理文件

FILE *fp=fopen("文件名”,打开形式);

fclose(fp);

例如:

FILE *fp=fopen("1.txt","r");

FILE *fp=fopen(argv[1],"w");

FILE *fp=fopen(argv[2],"a");

r:以只读的形式打开文件,文件打开成功,光标定位在文件开头,即定位在第一个数据脸上

w:以写的形式打开文件,若文件文件不存在,则创建文件然后打开,如果文件存在,则清空文件内容后打开。文件打开后,光标位于文件的末尾,即定位在最后一个数据的后面(文件结束符的脸上

a:以追加的形式打开文件,如果文件不存在,则创建文件然后后打开,如果文件存在,成功打开后,光标位于文件的末尾。

2.读写文件

fprintf:从文件中读取文件

int fprintf(FILE *stream, const char *format, ...)将format字符串中的内容,写入到stream文件指针指向的文件中去

int sprintf(char* str, const char *format, ...)将format中的内容,写入到 str指向的字符数组中去

 fprintf(fp,"%s",a);  //这里的a是数组名

fprintf("fp,"%d",&a);  //这里的a是变量a

fprintf(stdout,"hello world");   <===>printf("hello world);

fscanf:将内容写入文件中

int fscanf(FILE *stream, const char *format, ...)从文件指针stream中读取数据, 写入到format中格式占位符对应的数据中去

 fscanf(fp,"%s",a);

3.文件拷贝:实现从file1中读取数据,并写入file2中(不带空格和回车)

 其中读到文件末尾时,fscanf会返回-1

char a[128]={0};
while(1){
    int ret =fscanf(file1,"%s",a);
    fprintf(file2,"%s",a);
    if(ret!=1)
        break;
}

 4.读写文件(2)

int fgetc(FILE *stream);从stream指向的文件中,读取一个字符,如果读取失败,返回-1

int fputc(int c, FILE *stream);将字符 c 输出到stream所指向的文件中去

 5.文件拷贝

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    FILE* rfp = fopen(argv[1],"r");
    FILE* wfp = fopen(argv[2],"w");
    if(rfp == NULL || wfp == NULL){return 1;}

    while(1){
        // 循环用fgetc去读取rfp指向的文件,如果文件读取完毕,会返回-1。读取到的数据作为返回值返回
        int res = fgetc(rfp);
        if(res==EOF){break;}
        // 没有break说明读取到了数据,将读取到的数据,写入 wfp指向的文件中
        fputc(res,wfp);
    }

    fclose(rfp);
    fclose(wfp);
    return 0;
}

6.使用fgetc,计算文件中的行数

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
//计算文件有几行
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("1.txt","r");
	if(fp==NULL)
		return -1;
	int count=0;
	while(1){
		int ret=fgetc(fp);
		if(ret=='\n')
			count++;
		if(ret==-1)
			break;
	}


	fclose(fp);
	printf("%d\n",count);
	return 0;
}

7.输出错误信息

void perror(const char *s);

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("123","r");
	if(fp==NULL){
		perror("fpopen函数");
	//    char *errmsg=strerror(errno);
	//	fprintf(stderr,"fopen错误信息:%s\n",errmsg);
	}
	return 0;
}

二、作业

编写链表,链表内存入数据,使用 fprintf 将链表中所有的数据,保存到文件中 使用 fscanf 读取文件中的数据,写入链表中

使用 fprintf 将链表中所有的数据,保存到文件中

//将链表中的数据写入文件中
void write_p(link_p H){
		if(H==NULL){
		printf("头节点为空\n");
		return;
	}
	FILE *fp=fopen("5.txt","w");
	if(fp==NULL){
		return;
	}
	link_p p=H->next;
	while(p!=NULL){
		fprintf(fp,"%d",p->data);
		p=p->next;

	}
	fclose(fp);
}

使用 fscanf 读取文件中的数据,写入链表中

//将文件中的数据写入链表中
void write_s(link_p H){
		if(H==NULL){
		printf("头节点为空\n");
		return;
	}
	FILE *fp=fopen("6.txt","r");
	if(fp==NULL){
		return;
	}
	int a;
	while(1){
		int ret=fscanf(fp,"%d",&a);
		insert_head(H,a);
		if(ret==-1)
			break;
	}
	fclose(fp);

}

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值