第四十三讲 项目 文件操作

来源:互联网 发布:mysql数据库优化方案 编辑:程序博客网 时间:2024/05/20 18:51

任务和代码:

【项目1-由键盘到文件】
(1)从键盘输入一个文件名,以及一个以#结束的字符序列,将输入的字符保存到文件中去。

/**Copyright (c) 2016, CSDN学院*All rights reserved.*文件名:main.c*作者:DylanLiu*完成日期:2016/10/17*版本号:V1.0**问题描述:文件操作。*程序输出:。*/#include "stdio.h"#include "stdlib.h"int main(){    FILE *fp ;    char ch,fname[10];    printf("文件名:");    gets(fname);    if ((fp=fopen(fname, "w"))==NULL)    {        printf("can not open\n");        exit(0);    }    while ((ch=getchar())!='#')        fputc(ch, fp);    fclose(fp);    return 0;}

输出结果:建立一个名称为f1.dat的文件,文件目录在所保存的源码文件夹中。

任务和代码:

(2)设上题建立了名为f1.dat的文件,请将这个文件拷贝到一个名为f2.dat的文件中。

/**Copyright (c) 2016, CSDN学院*All rights reserved.*文件名:main.c*作者:DylanLiu*完成日期:2016/10/17*版本号:V1.0**问题描述:文件操作。*程序输出:。*/#include "stdio.h"#include "stdlib.h"int main(){    FILE *fp1,*fp2 ;    char c;    if ((fp1=fopen("f1.dat", "r"))==NULL)    {        printf("connot open\n");        exit(0);    }    if ((fp2=fopen("f2.dat", "w"))==NULL)    {        printf("connot open\n");        exit(0);    }    c=fgetc(fp1);    while (c!=EOF)    {        fputc(c,fp2);        c=fgetc(fp1);    }    fclose(fp1);    fclose(fp2);    return 0;}

任务和代码:(3)以下程序的功能是将文件file1.dat的内容输出到屏幕上并复制到文件file2.dat中,请补充完整。

/**Copyright (c) 2016, CSDN学院*All rights reserved.*文件名:main.c*作者:DylanLiu*完成日期:2016/10/17*版本号:V1.0**问题描述:文件操作。*程序输出:。*/#include "stdio.h"#include "stdlib.h"int main(){    FILE *fp1, *fp2;  //(1)    char ch;    fp1=fopen("file1.dat","r");    fp2=fopen("file2.dat","w");    while (!feof(fp1))    {        ch=fgetc(fp1);  //(2)        putchar(ch);        fputc(ch, fp2);  //(3)    }    fclose(fp1);    fclose(fp2) ;    return 0;}

【项目2-文件中的符号个数】
统计一个文本文件中数字、空格、字母出现的次数,以及文件的字节数,并将结果输出,文本文件名在程序中输入(请自建文本文件完成测试)。

/**Copyright (c) 2016, CSDN学院*All rights reserved.*文件名:main.c*作者:DylanLiu*完成日期:2016/10/20*版本号:V1.0**问题描述:统计一个文本文件中数字、空格、字母出现的次数,以及文件的字节数,并将结果输出,文本文件名在程序中输入。*程序输出:题目要求的输出。*///这里自己可以在此程序的目录下建立一个名为test.txt的文本文档#include <stdio.h>#include <ctype.h>int main(){    FILE *fp=NULL;    char ch;    int dig=0;    int blank=0, character=0;    fp=fopen("test.txt", "r");    while (!feof(fp)){        ch=getc(fp);        putchar(ch);        if (ch==' ') blank++;        if (isdigit(ch)) dig++;        else character++;    }    printf("\n字符:%d\n数字:%d\n空格:%d\n", character, dig, blank);    fclose(fp);    return 0;}

输出结果:

任务和代码:

【项目3-成绩统计】
文件english.dat(这个文件中的数据量,超出了你之前所有的体验)中已经有了学生的英语考试成绩数据。
(1)请编程从english.dat中读取数据,求出这次考试的平均成绩,并统计输出优秀人数和不及格人数。请在下面程序基础上填空完成:

#include <stdio.h>  #include <stdlib.h>  int main()  {      int score; //读入的成绩      int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数      double sum=0,ave; //sum: 成绩和,ave: 平均分      //以输入的方式(ios::in)打开文件      FILE ____(1)____;      fp=fopen(____(2)____);      if(fp==NULL)      {          printf("open error!\n");          exit(1);      }      while(fscanf(____(3)____)!=EOF)   //当读取成功……      {          count++;          sum+=score;          if(____(4)____)              excelent++;          else if(score<60)              ____(5)____;      }      ____(6)____;        //下面输出结果      ave=sum/count;      printf("总人数为:%d\n", count);      printf("平均成绩为:%.2f\n", ave);      printf("优秀人数:%d\n", excelent);      printf("不及格人数:%d\n", fail);      return 0;  }  

补全后的代码:

/**Copyright (c) 2016, CSDN学院*All rights reserved.*文件名:main.c*作者:DylanLiu*完成日期:2016/10/20*版本号:V1.0**问题描述:成绩的计算。*程序输出:成绩。*//* 60 70 80 90 78 89 65 55 80 为eglish.txt中的内容*//* 这里的eglish.txt建立在本程序的目录中 */#include <stdio.h>#include <stdlib.h>int main(){    int score; //读入的成绩    int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数    double sum=0,ave; //sum: 成绩和,ave: 平均分    //以输入的方式(ios::in)打开文件    FILE *fp=NULL;    fp=fopen("eglish.txt", "r");    if(fp==NULL)    {        printf("open error!\n");        exit(1);    }    while(fscanf(fp, "%d", &score)!=EOF)   //当读取成功……    {        count++;        sum+=score;        if(score>90)            excelent++;        else if(score<60)            fail++;    }    fclose(fp);    //下面输出结果    ave=sum/count;    printf("总人数为:%d\n", count);    printf("平均成绩为:%.2f\n", ave);    printf("优秀人数:%d\n", excelent);    printf("不及格人数:%d\n", fail);    return 0;}

输出结果:

任务和代码:

【项目4-算工资】
从文件salary.txt中读入工人的工号、基本工资、奖金,将奖金全部增加20%(好事)后,将工号、基本工资、奖金和应发工资(前项目之和)保存到文件salarylist.txt中。

/**Copyright (c) 2016, CSDN学院*All rights reserved.*文件名:main.c*作者:DylanLiu*完成日期:2016/10/20*版本号:V1.0**问题描述:工资的计算和统计。*程序输出:工资文件。*/#include<stdio.h>#include<stdlib.h>int main(void){    int num=0;//工号    double salary=0.0, award=0.0;    FILE *fInput=NULL, *fOutput=NULL;    fInput=fopen("salary.txt", "r");    if (fInput==NULL){        printf("cant not open this file!\a\n");        exit(1);    }    fOutput=fopen("salarylist.txt", "w");    if (fOutput==NULL){        printf("Can't write to the file!\a\n");        exit(1);    }    while((fscanf(fInput, "%d %lf %lf", &num, &salary, &award))!=EOF){        award *= 1.2;        fprintf(fOutput, "%d  %.2f  %.2f  %.2f\n", num, salary, award, salary+award);        //award *=1.2;        //fprintf(fpout,  "%d %.2f %.2f %.2f\n", num, salary, award, salary+award );    }    printf("succeed!Good Luck!\n");    fclose(fInput);    fclose(fOutput);    return 0;}

输出结果:

这里写图片描述

这里写图片描述

心得:在经过一段时间的学习之后,不知不觉就到了文件操作部分,编程能力和编程的思维都有很大的提升, 在此特意为自己设一个阶段标志。 最后 转一个链接来鞭策自己的学习:
http://blog.chinaunix.net/uid-22230080-id-1782492.html

0 0