交个朋友吧

面试日记|明朝万达

  • 明朝万达

接口功能: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;
}
posted @ 2023-05-18 14:37  PamShao  阅读(127)  评论(0)    收藏  举报