南京師範大学のトレーニングプログラムは、12を再テスト

111「!」エンドに大文字にすべて小文字になる文字列、その後、入力文字列の保存ディスクファイル「file.txtは」への出力からキーボード入力。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch;
    FILE *fp;
    if((fp=fopen("file.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    while((ch=getchar())!='!')
    {
        if(ch>='a'&&ch<='z')   //小写字母转换成大写字母
            ch=ch-32;
        fputc(ch,fp);     //存入文件中
        putchar(ch);      //输出到屏幕上
    }
    putchar('\n');
    fclose(fp);
    return 0;
}

結果:

112そこ2つのディスク・ファイルは、「A」と「B」であり、文字、新しいファイルにこれらの二つの(アルファベット順)ファイル、出力を組み合わせて、このために必要な情報「C」、外出先のラインを入れて

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char a[100],ch,temp;
    int i,j,k;
    FILE *fp1,*fp2,*fp3;
    if((fp1=fopen("file1.txt","r"))==NULL)   //将文件file1中的字母存放到数组a中
    {
        printf("can't open file!\n");
        exit(0);
    }
    i=0;
    while((ch=fgetc(fp1))!=EOF)   //若用!feof(fp1),后面会多复制空格,所以不能用
    {
        a[i++]=ch;
        putchar(ch);
    }
    fclose(fp1);

    if((fp2=fopen("file2.txt","r"))==NULL)  //将文件file2中的字母存放到数组a中
    {
        printf("can't open file!\n");
        exit(0);
    }
    while((ch=fgetc(fp2))!=EOF)
    {
        a[i++]=ch;
        putchar(ch);
    }
    fclose(fp2);

    a[i]='\0';
    for(j=0; j<i-1; j++)          //进行冒泡排序
        for(k=0; k<i-1-j; k++)
        {
            if(a[k]>a[k+1])
            {
                temp=a[k];
                a[k]=a[k+1];
                a[k+1]=temp;
            }
        }
    printf("\n%s",a);

    if((fp3=fopen("file3.txt","w"))==NULL)    //将数组a中的字母存到文件file3中
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(j=0; j<i; j++)
        fputc(a[j],fp3);
    fclose(fp3);

    putchar('\n');
    return 0;
}

結果:

113 5人の学生があり、各学生は、元のデータと計算された平均スコア、平均スコアを計算し、(数、名前、3コースのグレードを含む)、キーボードからの3つのコース、学生の入力データのスコアを持っていますディスクファイル「スタッド」に保存されています。

 

#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Student    //学生结构体
{
    long num;
    char name[15];
    float score[3];
    float avg;
} stud[N];
int main()
{
    int i,j;
    float sum;
    FILE *fp;
    for(i=0; i<N; i++)    //从键盘输入数据
    {
        sum=0;
        scanf("%ld %s ",&stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
        {
            scanf("%f",&stud[i].score[j]);
            sum+=stud[i].score[j];
        }
        stud[i].avg=sum/3.0;
    }

    if((fp=fopen("stud.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)      //写入文件stud.txt中
    {
        if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("stud.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("打印文件数据:\n");
    for(i=0; i<N; i++)      //从文件stud.txt中读出数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);
    return 0;
}

結果:

「スタッド」ファイル、成績平均点によってソートプロセスの例に学生114生徒データは、内の新しいファイル「stud_sort」に分類されています。

#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Student    //学生结构体
{
    long num;
    char name[15];
    float score[3];
    float avg;
} stud[N];
int main()
{
    int i,j;
    FILE *fp;
    struct Student temp;
    if((fp=fopen("stud.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("stud.txt文件中已有数据:\n");
    for(i=0; i<N; i++)      //从文件stud.txt中读出数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);

    for(i=0; i<N-1; i++) //进行冒泡排序
        for(j=0; j<N-i-1; j++)
            if(stud[j].avg>stud[j+1].avg)
            {
                temp=stud[j];
                stud[j]=stud[j+1];
                stud[j+1]=temp;
            }

    if((fp=fopen("stud_sort.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //写入stud_sort.txt文件中
    {
        if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("stud_sort.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("stud_sort.txt文件中的数据:\n");
    for(i=0; i<N; i++)      //从文件stud_sort.txt中读出数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);
    return 0;
}

結果:

115上記の例では、生徒の成績挿入プロセスの一種となっています。学生のコースのグレード3、プログラムは最初に、新たに挿入された学生の成績平均点を算出し、達成度によってそれを挿入を挿入し、挿入後に新しいファイルを作成します。

#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Student    //学生结构体
{
    long num;
    char name[15];
    float score[3];
    float avg;
} stud[N+1];
int main()
{
    int i,j;
    FILE *fp;
    struct Student temp;
    float sum=0;
    scanf("%ld %s ",&stud[3].num,stud[3].name);//输入一个学生的数据
    scanf("%f %f %f",&stud[3].score[0],&stud[3].score[1],&stud[3].score[2]);
    sum=stud[3].score[0]+stud[3].score[1]+stud[3].score[2];
    stud[3].avg=sum/3.0;

    if((fp=fopen("stud_sort.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }

    for(i=0; i<N; i++)      //从文件stud_sort.txt中读出数据
        fread(&stud[i],sizeof(struct Student),1,fp);
    fclose(fp);

    for(i=0; i<N; i++)  //进行冒泡排序
        for(j=0; j<N-i; j++)
            if(stud[j].avg>stud[j+1].avg)
            {
                temp=stud[j];
                stud[j]=stud[j+1];
                stud[j+1]=temp;
            }

    if((fp=fopen("stud_sort1.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N+1; i++)     //写入stud_sort1.txt文件中
    {
        if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("stud_sort1.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读出stud_sort1.txt文件中的数据:\n");
    for(i=0; i<N+1; i++)     //读出stud_sort1.txt文件中的数据
    {
        fread(&stud[i],sizeof(struct Student),1,fp);
        printf("%ld %s ",stud[i].num,stud[i].name);
        for(j=0; j<3; j++)
            printf("%.2f ",stud[i].score[j]);
        printf("%.2f\n",stud[i].avg);
    }
    fclose(fp);

    return 0;
}

結果:

116労働者のデータを入れてディスクファイル「従業員」があります。従業員名、従業員番号、性別、年齢、住所、給与、健康、教育などの各従業員のためのデータ、。これは、簡潔なドキュメントを構築するために労働者の賃金の別の個々のうちにその従業員の名前、給与情報が必要です。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
struct Employee    //职工结构体
{
    long num;
    char name[15];
    char sex;
    int age;
    char addr[20];
    int salary;
    char health[8];
    char clas[10];
} employee[N];

struct Emp   //简洁职工结构体
{
    char name[15];
    int salary;
} emp[N];

int main()
{
    FILE *fp;
    int i;
    printf("输入职工详细信息:\n");
    for(i=0; i<N; i++) //键盘输入职工详细信息
        scanf("%ld %s %c %d %s %d %s %s",&employee[i].num,employee[i].name,&employee[i].sex,&employee[i].age,employee[i].addr,&employee[i].salary,employee[i].health,employee[i].clas);

    if((fp=fopen("employee.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //写入employee.txt文件中
    {
        if(fwrite(&employee[i],sizeof(struct Employee),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("employee.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //读出employee.txt文件中的数据,并传数据
    {
        fread(&employee[i],sizeof(struct Employee),1,fp);
        strcpy(emp[i].name,employee[i].name);
        emp[i].salary=employee[i].salary;
    }
    fclose(fp);

    if((fp=fopen("emp.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)     //将数据写入emp.txt文件中
    {
        if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("emp.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("打印简略职工信息:\n");
    for(i=0; i<N; i++)     //读出emp.txt文件中的数据
    {
        fread(&emp[i],sizeof(struct Emp),1,fp);
        printf("%s %d\n",emp[i].name,emp[i].salary);
    }
    fclose(fp);

    return 0;
}

結果:

117は、テーマ「賃金のファイル」に労働者からのデータを削除した後、元のファイルに戻る保存しました。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3

struct Emp   //简洁职工结构体
{
    char name[15];
    int salary;
} emp[N];

int main()
{
    FILE *fp;
    int i,j;
    char n[15];   //要删除的职工的名字

    printf("输入职工工资文件数据:\n");
    for(i=0; i<N; i++)   //键盘输入职工工资文件数据
        scanf("%s %d",emp[i].name,&emp[i].salary);
    if((fp=fopen("emp.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N; i++)
    {
        if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);


    if((fp=fopen("emp.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读出职工工资文件数据:\n");
    for(i=0; i<N; i++)           //读出emp.txt文件中的数据
    {
        fread(&emp[i],sizeof(struct Emp),1,fp);
        printf("%s %d\n",emp[i].name,emp[i].salary);
    }
    fclose(fp);

    printf("输入要删除的职工的名字:\n");    //进行删除
    scanf("%s",n);
    for(i=0; i<N; i++)
        if(strcmp(emp[i].name,n)==0)
            break;
    for(j=i+1; j<N; j++)
    {
        strcpy(emp[j-1].name,emp[j].name);
        emp[j-1].salary=emp[j].salary;
    }

    if((fp=fopen("emp.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    for(i=0; i<N-1; i++)     //将前N-1个数据写入emp.txt文件中
    {
        if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
            printf("File write error!\n");
    }
    fclose(fp);

    if((fp=fopen("emp.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读出删除后的职工工资文件数据:\n");
    for(i=0; i<N-1; i++)     //读出emp.txt文件中的数据
    {
        fread(&emp[i],sizeof(struct Emp),1,fp);
        printf("%s %d\n",emp[i].name,emp[i].salary);
    }
    fclose(fp);

    return 0;
}

結果:

118キーボード(各ラインの不等長)から文字の複数のライン、入力ディスクファイルに格納。そして、小文字を書くことは、画面上で、文字を大文字に変換出力したファイルからデータを読み込みます。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int flag=1,i;
    char c,str[80];
    FILE *fp;
    if((fp=fopen("text.txt","w"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    while(flag==1)        //逐次输入字符串
    {
        printf("Input string:");
        gets(str);
        fprintf(fp,"%s",str);
        fprintf(fp,"\n");
        printf("Continue?");
        c=getchar();
        if(c=='N'||c=='n')
            flag=0;
        getchar();
    }
    fclose(fp);

    if((fp=fopen("text.txt","r"))==NULL)
    {
        printf("can't open file!\n");
        exit(0);
    }
    printf("读取文件中的字符串,将小写字母转换成大写字母\n");
    while(fscanf(fp,"%s",str)!=EOF)    //读取字符串
    {
        for(i=0; str[i]!='\0'; i++)
            if(str[i]>='a'&&str[i]<='z')
                str[i]-=32;
        printf("%s\n",str);
    }
    fclose(fp);

    return 0;
}

結果:

 

公開された462元の記事 ウォン称賛55 ビュー320 000 +

おすすめ

転載: blog.csdn.net/LY_624/article/details/105120977