
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void JA(char* fname)
{
FILE* fout=fopen(fname,"w");
char a[20];
printf("输入若干个字符串,串长小于20,字符串end作为结束标志\n");
while(1) {
scanf("%s",a);
if(strcmp(a,"end")==0) break;
fputs(a,fout);
fputc('\n',fout);
}
fclose(fout);
}
void main()
{
char *p="d:\\xxk\\xuxk1.txt";
JA(p);
}
请输入运行结果:
#include<stdio.h>
int JB(char* fptr)
{
char a[20],*p;
int c=0;
FILE* fin=fopen(fptr,"r");
while(!feof(fin)) {
p=fgets(a,20,fin);
if(p==NULL) break;
else {printf("%s",a); c++;}
}
return c;
}
void main()
{
char *p="d:\\xxk\\xuxk1.txt";
printf("文件内容的总行数: %d\n",JB(p));
}
请输入运行结果:
#include<stdio.h>
struct Student {
char num[6]; //学号
char name[10]; //姓名
int grade; //分数
};
struct Student x,y;
struct Student a[4]={{"100","xxk",85},{"102","wrong",83},
{"103","xcong",88},{"104","bjuan",73}};
void main()
{
int max=0;
char *p="d:\\xxk\\xuxk2.bin";
FILE* fio=fopen(p, "wb+");
fwrite(a, sizeof(struct Student), 4, fio);
fseek(fio,0,SEEK_SET);
while(!feof(fio)) {
fread(&x, sizeof(struct Student), 1, fio);
if(feof(fio)) break;
printf("%s %s %d\n", x.num, x.name, x.grade);
if(x.grade>max) {max=x.grade; y=x;}
}
printf("\n具有最高分数的记录: %s %s %d\n",y.num,y.name,y.grade);
fclose(fio);
}
请输入运行结果: