zoukankan      html  css  js  c++  java
  • 实验十一 文件操作 程序一

    程序一

    用记事本建立文件src.dat,其中存放若干字符。编写程序,从文件src.dat中读取数据,统计其中的大写字母、小写字母、数字、其它字符的个数,并将这些数据写入到文件test.dat中。

     请在D盘新建文件夹wj,并新建文本文档src.dat和test.dat然后可以在src.dat中输入字符。

    #include <stdio.h>
    #include<stdlib.h>
    int main(int argc, char *argv[])
    {
    FILE *fp;
    char ch;
    int daxie=0,xiaoxie=0,shuzi=0,qita=0;
    if((fp=fopen("D:\wj\src.dat.txt","r"))==NULL){
    printf("File open error! ");
    exit(0);
    }
    while(!feof(fp)){
    ch=fgetc(fp);
    if(ch>='A'&&ch<='Z'){
    daxie++;
    }
    else if(ch>='a'&&ch<='z'){
    xiaoxie++;
    }
    else if(ch>='0'&&ch<='9'){
    shuzi++;
    }
    else {
    qita++;
    }
    }
    if((fp=fopen("D:\wj\test.dat.txt","w"))==NULL){
    printf("File open error! ");
    exit(0);
    }
    fprintf(fp,"大写字母=%d 小写字母=%d 数字=%d 其他=%d ",daxie,xiaoxie,shuzi,qita-1);
    if(fclose(fp)){
    printf("Can not close the file! ");
    exit(0);
    }
    printf("操作已完成! ");
    return 0;
    }

  • 相关阅读:
    第一周C语言作业
    C语言I博客园作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
  • 原文地址:https://www.cnblogs.com/ars134419622/p/10179101.html
Copyright © 2011-2022 走看看