gets和fgets区别:
1)gets没有给定最多读取字符的个数,有越界风险\n\n
fgets需要给定最多读取的字符个数,没有越界的风险\n\n
2)gets会去掉从终端接收的/n,换成/0\n\n fgets则会保留并在末尾加上/0\n\n
3)puts会在字符串末尾多打印一个/n字符\n\n
fputs不会在末尾多打印/n字符
函数接口:
1.fwrite
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:
向流中写入nmemb个对象,每个对象size字节大小,在ptr指向的空间中
参数:
ptr:存放数据空间的首地址
size:每个数据对象的大小
nmemb:数据对象的个数
stream:文件流指针
返回值:
成功返回写入对象的个数
失败返回0
读到文件末尾返回0
练习:
将多个对象写入到指定文件中
#include <stdio.h>typedef struct student
{char name[32];char sex;int age;int score;
}stu_t;int main (void)
{stu_t a = {"zhangsan",'m',18,167};stu_t s[3]={{"lisi",'f',17,155},{"wamngliu",'m',18,167},{"jiuqi",'m',17,163},};FILE *fp = NULL;fp = fopen("file.txt","w");if (fp == NULL){perror("file to fopen");return -1;}fwrite(&a,sizeof(stu_t),1,fp);fwrite(s,sizeof(stu_t),3,fp);fclose(fp);return 0;
}
2.fread
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:
从流中读取nmemb个对象,每个对象size个字节,存放到ptr指向的空间中
参数:
ptr:存放读取内容空间首地址
size:读取对象的大小
nmemb:读取对象的个数
stream:文件流指针
返回值:
成功返回读到对象个数
失败返回0
读到文件末尾返回0
练习:
将指定文件中的内容读出来并展示在终端上
#include <stdio.h>typedef struct student
{char name[32];char sex;int age;int score;
}stu_t;int ShowStuInfo(stu_t tmpstu)
{printf("姓名:%s\n",tmpstu.name);printf("性别:%c\n",tmpstu.sex);printf("年龄:%d\n",tmpstu.age);printf("成绩:%d\n",tmpstu.score);return 0;
}int main (void)
{FILE *fp = NULL;stu_t s[5];size_t ret;int i = 0;stu_t a;fp = fopen("file.txt","r");if (fp == NULL){perror("file to fopen");return -1;}fread(&a,sizeof(stu_t),1,fp);ShowStuInfo(a);ret = fread(s,sizeof(stu_t),5,fp);for (i = 0;i < ret;++i){ShowStuInfo(s[i]);}printf("ret = %ld\n",ret);fclose(fp);return 0;
}
练习:
利用fread和fwrite完成将src.jpg图片内容拷贝到dst.jpg图片中 char tmpbuff[4096];
#include <stdio.h>int main(void)
{FILE *fp = NULL;FILE *fq = NULL;char s[4096];size_t ret = 0;fp = fopen("src.jpg","r");fq = fopen("dst.jpg","w");if (fp == NULL){perror("file to fopen");return -1;}if (fq == NULL){perror("file to fopen");return -1;}while(1){ret = fread(s,1,sizeof(s),fp);if (ret == 0){break;}fwrite(s,1,ret,fq);}fclose(fp);fclose(fq);return 0;
}
3.fprintf
int fprintf(FILE *stream, const char *format, ...);
功能:
将格式化字符串输出到stream指向的流中
printf
fprintf(stdout, );
练习:
#include <stdio.h>int main (void)
{FILE *fp = NULL;int num1 = 100;int num2 = 200;fp = fopen("file.txt","w");if(fp == NULL){perror("file to fopen");return -1;}fprintf(fp,"hello world! num1 = %d num2 = %d\n",num1,num2);fclose(fp);return 0;
}
4.fscanf
int fscanf(FILE *stream, const char *format, ...);
功能:
从流中读取格式化的字符串
流的定位
1.ftell
long ftell(FILE *stream);
功能:
获得流的偏移量
2.rewind
void rewind(FILE *stream);
功能:
将流的偏移量重新设置到开头
3.fseek
int fseek(FILE *stream, long offset, int whence);
功能:
设置流的偏移量
参数:
stream:文件流指针
offset:偏移量
> 0 向后偏移
< 0 向前偏移
whence:
SEEK_SET 文件开头
SEEK_CUR 文件当前位置
SEEK_END 文件末尾
练习:
#include <stdio.h>int main(void)
{FILE *fp = NULL;fp = fopen("file.txt","w");if (fp == NULL){perror("file to fopen");return -1;}fseek(fp,10,SEEK_SET);fprintf(fp,"hello");fseek(fp,-5,SEEK_CUR);fprintf(fp,"app");fseek(fp,6,SEEK_END);fprintf(fp,"world");fclose(fp);return 0;
}
练习:
编写一个程序实现统计一个文件的大小
#include <stdio.h>int main (void)
{FILE *fp = NULL;int len = 0;fp = fopen("file.txt","r");if (fp == NULL){perror("file to fopen");return -1;}fseek(fp,0,SEEK_END);len = ftell(fp);printf("len = %d\n",len);fclose(fp);return 0;
}
作业:
从终端输入一个单词,获得单词的含义 含义在文件dict.txt中
int main (void)
{char s[30]; //定义一个数组接收需要查找的单词char timp[4096]; //定义一个数组接收一行字符串char *ptmp = NULL; char *pret = NULL; printf("Enter a word: \n");gets(s); //输入要查找的单词FILE *fp = NULL;fp = fopen("dict.txt","r"); //打开并读取这个文件if (fp == NULL){perror("file to fopen");return -1;}while(1){pret = fgets(timp,sizeof(timp),fp); //进入循环并用pret接收这一行字符串的地址if (pret == NULL) { //若pret为空说明最后一行都读完了break;}ptmp = timp; //定义一个指针指向接收的一行字符串的开头while (*ptmp != ' ' && *ptmp != '\0') {ptmp++; //进入循环 如果指针没有指向了空格和'\0',就继续向后移动 遇到空格或‘0’就跳出}*ptmp = '\0'; //在遇到空格的位置补'\0'ptmp++; //指针在继续往后移动一位while (*ptmp != ' ') //进入循环 如果指针没有指向了空格{ptmp++; //继续往后移动 直到跳出 并分成了单词和后面注释这两部分 此时ptmp就为注释的这个字符串的首地址}if (strcmp(s,timp) == 0) //比较这两个单词 如果相等{printf("ptmp:%s\n",ptmp); //打印出后面的注释fclose(fp); return 0; //关闭流 退出}}printf("%s不存在!\n",s); fclose(fp);return 0;
}