#include<stdio.h>#include<string.h>int_filesize(FILE*stream){int len;fseek(stream,0L,2);//移动光标到文档尾部
len=ftell(stream);//返回光标距离文件头部的字节数 fseek(stream,0L,0);return len;}intmain(){
FILE *stream;//文件流指针
stream=fopen("a.txt","r+");//打开文件放入流中if(stream){//流读取成功时,返回指针;否则返回NULL char txt[100]={0};fscanf(stream,"%[^\n]",txt);//流中的内容放入字符串中printf("当前文档内容:%s\n",txt);printf("当前文档字节数:%d\n",_filesize(stream));fprintf(stream,"I love you!\n");//向文件中写入 "I love you!"fclose(stream);//关闭文件 }else{printf("打开文档失败");}return0;}
函数介绍
基本结构
FILE *stream;
stream=fopen("a.txt","r+");//打开文件放入流中if(stream){//流读取成功时,返回指针;否则返回NULL fscanf(stream,"%[^\n]",txt);//流中的内容放入字符串中fprintf(stream,"this is a test!\n");//向文件中写入 "this is a test!"fclose(stream);//关闭文件 }else{printf("打开文档失败");}
函数介绍
FILE *流指针 =fopen("文件地址和名称","读写方式")fscanf(stream,"%[^\n]",txt);//流中的内容放入字符串中fscanf(流指针,"输入格式控制字符",变量地址)fprintf(stream,"this is a test!\n");//向文件中写入 "this is a test!"fprintf(流指针,"写入文件内容");//向文件中写入 "this is a test!"fprintf(stream,"%s",ss);//将ss字符串中的内容写入文件 fprintf(流指针,"输出格式控制字符",变量);fclose(stream);//关闭文件 fclose(流指针);fseek(流指针,从基准位置移动字节数L,基准位置);//移动光标
定位位置:0=>文首 ; 1=>当前位置 ;2=>文末
intftell(流指针);//返回光标距离文件头部的字节数