#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
int no;
char name[100];
float score;
}stu[100];
int cmp(const void *a ,const void *b)
{
return (*(struct student *)b).score-(*(struct student *)a).score;
}
void Atoa(char s[])
{
int flag=0;
int lenth=strlen(s);
for(int i=0;i<lenth;i++)
{
if(s[i]>='a'&&s[i]<='z')
s[i]=s[i]+'A'-'a';
}
}
12.3
int main(int argc, char const *argv[])
{
FILE *fp;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r+"))==NULL)
{
printf("fail\n");
exit(0);
}
char string[100];
while(gets(string))
{
//printf("%s\n",string);
Atoa(string);
printf("%s\n",string);
fprintf(fp,"%s\n",string);
fprintf(fp, "%c\n",'\t');
fprintf(fp, "%s\n", string);
}
fclose(fp);
}
//12.4
void inverse(char s[])
{
printf("jinru inverse %s\n",s);
printf("%c %c\n",s[strlen(s)-1],s[strlen(s)-2]);
for (int i = 0; i < strlen(s)/2; ++i)
{
char x=s[i];
s[i]=s[strlen(s)-i-2];//key
s[strlen(s)-i-2]=x;
}
printf("jieshuinverse %s\n",s);
}
int main(int argc, char const *argv[])
{
FILE *fp,*fp2;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r+"))==NULL)
{
printf("fail\n");
exit(0);
}
if((fp2=fopen("/Users/hellooks/Desktop/fileio/n.txt","w+"))==NULL)
{
printf("fail\n");
exit(0);
}
char string[100];
while(fgets(string,100,fp))
{
//printf("%s\n",string);
inverse(string);
//printf("%s\n",string);
fprintf(fp2, "%s",string);
}
fclose(fp);fclose(fp2);
return 0;
}
//12.5
int main(int argc, char const *argv[])
{
int count=0;
char c;
FILE *fp;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r"))==NULL)
{
printf("fail\n");
exit(0);}
int flag=0;
while((c=fgetc(fp))!=EOF)
{
if(c>='a'&&c<='z')
{
if(flag==0)
{count++;flag=1;}
if(flag==1) continue;
}
else {
if(flag==1) flag=0;
else continue;
}
}
printf("%d\n",count);
fclose(fp);
return 0;
}
// 12.6
int main(int argc, char const *argv[])
{
FILE *fp,*fp2;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r+"))==NULL)
{
printf("fail\n");
exit(0);
}
if((fp2=fopen("/Users/hellooks/Desktop/fileio/n.txt","w"))==NULL)
{
printf("fail\n");
exit(0);
}
fputc('a',fp);
fputc('\t',fp);
fputc('a',fp);
fputc('\t',fp);fputc('a',fp);
fputc('\t',fp);fputc('a',fp);
fputc('\t',fp);fputc('a',fp);
fputc('\t',fp);fputc('a',fp);
fputc('\t',fp);fputc('a',fp);
fputc('\t',fp);fputc('a',fp);
fputc('\t',fp);
char c;
fseek(fp,0,SEEK_SET);
while((c=fgetc(fp))!=EOF)
{
if(c=='\t')
{
fputc(' ',fp2);
fputc(' ',fp2);
fputc(' ',fp2);
fputc(' ',fp2);
}
else
{
fputc(c,fp2);
}
}
return 0;
}
//12.7
struct student{
int no;
char name[100];
float s1;
float s2;
float s3;
}stu[100];
int main(int argc, char const *argv[])
{
FILE *fp;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r+"))==NULL)
{
printf("fail\n");
exit(0);
}
int flag=1;
int i=0;
struct student stud;
// while((fread(&stud,sizeof(struct student),1,fp))==1)
// {
// printf("%s\n",stud.name);
// }}
while(flag!=0)
{
scanf("%s %d %f %f %f",stu[i].name,&stu[i].no,&stu[i].s1,&stu[i].s2,&stu[i].s3);
printf("jixushuruma \n");
scanf("%d",&flag);
i++;
}
for(int j=0;j<i;j++)
{
fwrite(&stu[j],sizeof(struct student),1,fp);
}
printf("shuruwancheng\n");
return 0;
}
//12.8
struct student{
int no;
char name[100];
float s1;
float s2;
float s3;
float sum;
}stu[100];
int cmp(const void *a,const void *b)
{
return (*(struct student *)b).sum-(*(struct student *)a).sum;
}
int main(int argc, char const *argv[])
{
FILE *fp;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r+"))==NULL)
{
printf("fail\n");
exit(0);
}
int flag=1;
int i=0;
struct student stud;
while((fread(&stud,sizeof(struct student),1,fp))==1)
{
strcpy(stu[i].name,stud.name);
stu[i].no=stud.no;
stu[i].s1=stud.s1;
stu[i].s2=stud.s2;
stu[i].s3=stud.s3;
stu[i].sum=stud.sum;
i++;
}
for(int j=0;j<i;j++)
printf("%f\n",stu[j].sum);
qsort(stu,i,sizeof(stu[0]),cmp);
for(int j=0;j<i;j++)
fwrite(&stu[j],sizeof(struct student),1,fp);
// while(flag!=0)
// {
// scanf("%s %d %f %f %f",stu[i].name,&stu[i].no,&stu[i].s1,&stu[i].s2,&stu[i].s3);
// printf("jixushuruma \n");
// scanf("%d",&flag);
// stu[i].sum=stu[i].s1+stu[i].s2+stu[i].s3;
// i++;
// }
// for(int j=0;j<i;j++)
// {
// fwrite(&stu[j],sizeof(struct student),1,fp);
// }
// printf("shuruwancheng\n");
fclose(fp);
// 写到另外一文件中去 用fp2 自己写
return 0;
}
//12.9
struct student{
int no;
char name[100];
float s1;
float s2;
float s3;
}stu[100];
int main(int argc, char const *argv[])
{
FILE *fp,*fp1,*fp2;
if((fp=fopen("/Users/hellooks/Desktop/fileio/new2.txt","r+"))==NULL)
{
printf("fail\n");
exit(0);
}
if((fp1=fopen("/Users/hellooks/Desktop/fileio/n.txt","w+"))==NULL)
{
printf("fail\n");
exit(0);
}
if((fp2=fopen("/Users/hellooks/Desktop/fileio/oushu.txt","w+"))==NULL)
{
printf("fail\n");
exit(0);
}
int flag=1;
int i=0;
struct student stud;
while((fread(&stud,sizeof(struct student),1,fp))==1)
{
printf("%s\n",stud.name);
if(i++%2==0)
{fwrite(&stud,sizeof(struct student),1,fp2);
printf("oushu\n");
}
else {
fwrite(&stud,sizeof(struct student),1,fp1);
}
}
remove("/Users/hellooks/Desktop/fileio/new2.txt");
rename("/Users/hellooks/Desktop/fileio/n.txt","/Users/hellooks/Desktop/fileio/new2.txt");
}
//12.10
struct words{
char a[100];
}wd[1000];
int cmp(const void *aa, const void *bb)
{
return strcmp((*(struct words*)aa).a,(*(struct words*)bb).a);
}
int main(int argc, char const *argv[])
{
FILE *fp1,*fp2,*fp3;
if((fp1=fopen("/Users/hellooks/Desktop/fileio/1.txt","r"))==NULL)
{
printf("fail\n");
}
if((fp2=fopen("/Users/hellooks/Desktop/fileio/2.txt","r"))==NULL)
{
printf("fail\n");
}
if((fp3=fopen("/Users/hellooks/Desktop/fileio/3.txt","w+"))==NULL)
{
printf("fail\n");
}
char ch;
char word[1000];
int i=0;
while((ch=fgetc(fp1))!=EOF)
{
word[i++]=ch;
}
word[i++]=' ';//key i++ 不行
while((ch=fgetc(fp2))!=EOF)
{
word[i++]=ch;
}
printf("%s\n",word);
int flag=0;
int q=0;
int k=0;
for(int j=0;j<i;j++)
{
printf("%c\n",word[j] );
if((word[j]>='a'&&word[j]<='z')||(word[j]>='A'&&word[j]<='Z'))
{
if(flag==0)
{
flag=1;
q=0;
wd[k].a[q++]=word[j];
}
else
{
wd[k].a[q++]=word[j];
}
}
else
{
if(flag==1)
{
flag=0;
k++;
}
}
}
printf(" \n");
for(int w=0;w<k;w++)
printf("%s\n",wd[w].a);
qsort(wd,k,sizeof(struct words),cmp);
for(int w=0;w<k;w++)
printf("%s\n",wd[w].a);
for(int i=0;i<k;i++)
{
fwrite(&wd[i],sizeof(struct words),1,fp3);
}
fseek(fp3,0,SEEK_SET);
struct words aaa;
while(fread(&aaa,sizeof(struct words),1,fp3)==1)
{printf("文件3%s\n",aaa.a);}
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}