面试日记|明朝万达

  • 明朝万达

接口功能:C实现读取TXT文件,并将数字和字母单独提出来。

程序:

#include <stdio.h>
int j=0,k=0;

void read_TXT(const char *path,char *string,char *num)
{
    char file;
    FILE* fpRead = fopen(path, "r");
    if (fpRead == NULL)
    {
        printf("打开文件失败!\n");
        return;
    }
    while(fscanf(fpRead, "%c ", &file)==1)
    {
        if (file <= '9' && '0' <= file)
        {
            num[j] = file;
            j++;
        }
        if ((file <= 'Z' && 'A'<=file) || (file <= 'z' && 'a'<=file))
        {
            string[k] = file;
            k++;
        }
    }
    fclose(fpRead);
}
int main()
{
    //读数据,将读到的数据存到数组a[]中
    char string[100];
    char num[100];
    char *path="/Users/hangshao/Desktop/read/demo.txt";

    read_TXT(path,string,num);

    for(int i=0;i<j;i++)
    {
        printf("%c",num[i]);
    }
    printf("\n\n");
    for(int i=0;i<k;i++)
    {
        printf("%c",string[i]);
    }
    return 1;
}

接口功能:C实现读取TXT文件,统计指定单字符的数量。

程序:

#include <stdio.h>

//读取TXT文本,统计指定单字符出现次数
int Coun_Num(const char *path,const char s)
{
    int count=0;
    char file;
    FILE *fp=fopen(path,"r");
    if(fp==NULL)
    {
        printf("no \n");
        return 0;
    }
    while(fscanf(fp,"%c",&file)==1)
    {
        if(file == s)
        {
            count++;
        }
    }
    fclose(fp);
    return count;
}

int main() {

    printf("%d \n",Coun_Num("/Users/hangshao/Desktop/untitled1/demo.txt",'s'));
    return 0;
}

作者:Hang Shao

出处:https://www.cnblogs.com/pam-sh/p/17411913.html

版权:本作品采用「知识共享」许可协议进行许可。

声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.

posted @   PamShao  阅读(108)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-05-18 SM3和Blake
2020-05-18 C:习题1
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题