2、字符串读/写
函数:fputs、fgets
格式:
fputs(str,fp);
把str指向的字符串写到fp指向的文件中
fgets(str,length,fp);
从fp指向的文件中读取length-1字符,放到str中
(遇到回车或换行符结束,如遇到换行符结束则读的字符串中包含换行符)
如读到文件结束标记,fgets函数返回NULL
读任意行的文件:
while (fgets(str,length,fp))
{
//对读取到的字符串进行处理
}
注意:适合于文本文件
*/
/*
实例:建立一个文本文件,存储10个学生姓名,然后读取文件内容,以行为单位输出所有人的姓名
分析:
设计一个建立文件的函数write()
设计一个读取文件的函数read()
void write()
{
FILE *fp;
int i;
char st[20];
fp=fopen("d:\\test\\xm.txt","w");
for(i=0;i<10;i++)
{
gets(st);
fputs(st,fp);
fputc('\n',fp);
}
fclose(fp);
}
void read()
{
FILE *fp;
char st[20];
fp=fopen("d:\\test\\xm.txt","r");
while (fgets(str,length,fp))
{
printf("%s",st);
}
fclose(fp);
}
*/
/*
#include <stdio.h>
void write()
{
FILE *fp;
int i;
char st[20];
fp=fopen("d:\\test\\xm.txt","w");
for(i=0;i<10;i++)
{
gets(st);
fputs(st,fp);
fputc('\n',fp);
}
fclose(fp);
}
void read()
{
FILE *fp;
char st[20];
fp=fopen("d:\\test\\xm.txt","r");
while (fgets(st,20,fp))
{
printf("%s",st);
}
fclose(fp);
}
int main()
{
//write();
read();
return 0;
}
*/
/*
拓展:统计一个xm.txt中有没有姓名为zhangsan的学生,如有输出他在班里的序号(1,2,3,...),
否则输出no find!
分析:
设计一个函数统计count()
void count()
{
FILE *fp;
char st[20];
int i=0,length,flag=0;
fp=fopen("d:\\test\\xm.txt","r");
while (fgets(st,20,fp))
{
i++;
if (strcmp(st,"zhangsan")==0) {flag=1;break;}
}
fclose(fp);
if (flag==0)
return 0;
else
return i;
}
*/
/*
#include <stdio.h>
#include <string.h>
int count()
{
FILE *fp;
char st[20];
int i=0,length,flag=0;
fp=fopen("d:\\test\\xm.txt","r");
while (fgets(st,20,fp))
{
i++;
//把st后边的换行符去掉
length=strlen(st);
st[length-1]='\0';
if (strcmp(st,"lisi")==0) {flag=1;break;}
}
fclose(fp);
if (flag==0)
return 0;
else
return i;
}
int main()
{
int k;
k=count();
if (k==0)
printf("no find!\n");
else
printf("%d\n",k);
return 0;
}
*/
/*
问题:15张选票,三个候选人(库里,哈登,字母哥),统计谁是票王?
分析:
struct person
{
char name[20];
int cnt;
};
设计一个函数建立选票文件write()
void write()
{
FILE *fp;
int i;
char st[20];
fp=fopen("d:\\test\\xp.txt","w");
for(i=0;i<10;i++)
{
gets(st);
fputs(st,fp);
fputc('\n',fp);
}
fclose(fp);
}
设计一个统计函数(统计每个人的选票)
void count(struct person tt[],int n)
{
//(1)打开文件
//(2)读取选票并统计
FILE *fp;
char st[20];
int i,length;
fp=fopen("d:\\test\\xp.txt","r");
while (fgets(st,20,fp))
{
length=strlen(st);
st[length-1]='\0';
for(i=0;i<n;i++)
if (strcmp(tt[i].name,st)==0) tt[i].cnt++;
}
fclose(fp);
}
设计一个函数统计谁是票王?
*/
#include <stdio.h>
#include <string.h>
struct person
{
char name[20];
int cnt;
};
void write()
{
FILE *fp;
int i;
char st[20];
fp=fopen("d:\\test\\xp.txt","w");
for(i=0;i<15;i++)
{
gets(st);
fputs(st,fp);
fputc('\n',fp);
}
fclose(fp);
}
void count(struct person tt[],int n)
{
//(1)打开文件
//(2)读取选票并统计
FILE *fp;
char st[20];
int i,length;
fp=fopen("d:\\test\\xp.txt","r");
while (fgets(st,20,fp))
{
length=strlen(st);
st[length-1]='\0';
for(i=0;i<n;i++)
if (strcmp(tt[i].name,st)==0) tt[i].cnt++;
}
fclose(fp);
}
struct person findpw(struct person tt[],int n)
{
struct person st;
int i;
st=tt[0];
for(i=1;i<n;i++)
if (tt[i].cnt>st.cnt) st=tt[i];
return st;
}
int main()
{
struct person leader[3]={"库里",0,"哈登",0,"字母哥",0};
struct person pw;
//write();
count(leader,3);
pw=findpw(leader,3);
printf("%s,%d\n",pw.name,pw.cnt);
return 0;
}