一、打开、读写、关闭
1.1 fopen1.2 fclose1.3 fwrite1.4 fread
文件读写指针 二、fseek、feof、ftell
实现写读写一行
写读(feof、ftell、rewind) 作业:实现将一个文件拷贝给另一个文件fgetc函数、fputc函数fprintf文件 *** 作(stdio、stdout、stderr)随机读取数据
一、打开、读写、关闭 1.1 fopen一般用a+
#include#include int main(int argc, char const *argv[]) { FILE *fp; //fp = fopen("a.txt","w+"); fp = fopen(argv[1],"a+"); if (NULL == fp) { printf("file a.txt error!n"); exit(1); } return 0; }
1.2 fclose加b可以以二进制打开文件,不加为字符打开
FILE *fp; fp = fopen(argv[1],"a+"); fclose(fp);1.3 fwrite
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
const void *ptr:要写入的数据指针
size_t size:每次写size字节
size_t nmemb:总共写nmemb次
FILE *stream:所写入的文件
返回值:nmemb(写了多少次)
出错是0
#include1.4 fread#include #include int main(int argc, char const *argv[]) { FILE *fp; //fp = fopen("a.txt","w+"); fp = fopen(argv[1],"a+"); //fp = fopen(argv[1],"r"); if (NULL == fp) { printf("file a.txt error!n"); exit(1); } char buffer[1024] = "hello world"; int ret = fwrite(buffer,1,strlen(buffer),fp); printf("ret = %dn",ret); return 0; }
size_t fread(void *ptr, size_t size, size_t nmemb,FILE *stream);
void *ptr:要写入的数据指针
size_t size:每次读size字节
size_t nmemb:总共读nmemb次
FILE *stream:所写入的文件
返回值:nmemb(读了多少次)
出错是0
#include文件读写指针#include #include int main(int argc, char const *argv[]) { FILE *fp; //fp = fopen("a.txt","w+"); fp = fopen(argv[1],"a+"); if (NULL == fp) { printf("file a.txt error!n"); exit(1); } char buffer[1024] = "hello world"; int ret = fwrite(buffer,1,strlen(buffer),fp); printf("ret = %dn",ret); memset(buffer,0,sizeof(buffer)); ret = fread(buffer,1,ret,fp); buffer[ret] = ''; printf("read buffer = %sn",buffer); return 0; } //读不出来,是因为文件读写位置指针指向末尾
int fseek(FILE *stream, long offset, int whence);
FILE *stream 目标文件
long offset 偏移量(正数是后移,负数是前移)
int whence 锁定位置(配图)
顺序:先锁定位置,再判断偏移量
#include实现写读写一行 写#include #include int main(int argc, char const *argv[]) { FILE *fp; //fp = fopen("a.txt","w+"); fp = fopen(argv[1],"a+"); if (NULL == fp) { printf("file a.txt error!n"); exit(1); } char buffer[1024] = "hello world"; int ret = fwrite(buffer,1,strlen(buffer),fp); if(0 == ret) { printf("fwrite a.txt error!n"); exit(1); } printf("ret = %dn",ret); ret = fseek(fp,0,SEEK_SET); if(-1 == ret) { printf("fseek a.txt error!n"); exit(1); } memset(buffer,0,sizeof(buffer)); ret = fread(buffer,1,ret,fp); if(0 == ret) { printf("fread a.txt error!n"); exit(1); } buffer[ret] = ''; printf("read buffer = %sn",buffer); return 0; }
fwrite("n",1,1,fp);读(feof、ftell、rewind)
文档结尾有个隐藏字符EOF
int feof(FILE *stream);
文件结束:返回非0值;文件未结束:返回0值
ftell:用来得到文件指针离文件开头的偏移量
long ftell(FILE *stream);
rewind()用于文件指针移动到文件的开头,当移动成功时
void rewind(FILE *stream);
#include作业:实现将一个文件拷贝给另一个文件 fgetc函数、fputc函数#include #include int main(int argc, char const *argv[]) { FILE *fp; //fp = fopen("a.txt","w+"); fp = fopen(argv[1], "a+"); if (NULL == fp) { printf("file a.txt error!n"); exit(1); } char buffer[1024] = "hello world"; int ret = fwrite(buffer, 1, strlen(buffer), fp); fwrite("n", 1, 1, fp); if (0 == ret) { printf("fwrite a.txt error!n"); exit(1); } printf("ret = %dn", ret); ret = fseek(fp, 0, SEEK_SET); if (-1 == ret) { printf("fseek a.txt error!n"); exit(1); } memset(buffer,0,sizeof(buffer)); char temp; int i = 0; while (!feof(fp)) { if(fread(&temp, 1, 1, fp) == -1) { printf("file fread error!n"); } buffer[i] = temp; i++; } #if 0 ret = fread(buffer,1,ret,fp); if(0 == ret) { printf("fread a.txt error!n"); exit(1); } #endif buffer[i] = ''; printf("read buffer = %sn",buffer); fclose(fp); return 0; }
FILE from_fp char temp; while(temp = fgetc(from_fp) != EOF) { putc(temp,to_fp) }
fgets(读一行)
fprintf
文件里面没格式,需要自己去定义
int num = 5; char FILE *fp = fopen(atgv[1],"a+"); fprintf(fp,"%d:%s:%dn",num,name,age);文件 *** 作(stdio、stdout、stderr)
文件指针:
stdio(键盘)
stdout(终端)
stderr(终端)
char buffer[1024]; fgets(buffer,sizeof(buffer),stdin); buffer[strlen(buffer) - 1] = ''; fputs(buffer,stdout); 打印错误信息或者调试信息时,用stderr 打印正常信息时,用stdout随机读取数据
二进制读取文件
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)