鱼C论坛

 找回密码
 立即注册
查看: 4475|回复: 7

[已解决]新人刚学文件操作有一题请教大佬

[复制链接]
发表于 2020-11-30 16:59:49 | 显示全部楼层 |阅读模式
5鱼币
这个代码要求编写函数ext\fractDigit,该函数从已有的当前目录下的文件a.txt中读取并解析出其中的数值,并将结果写到当前目录下的文件b.txt中。

其中:文件a.txt中包含各种字符,但只有数字字符是有效的,提取其中的数字字符,将每3个数字组成一个整数,写到文件b.txt中,每个整数之间用一个空格分开。读取过程中,如果a.txt中最后剩下不到3个数字字符,则将剩下的1个或者2个数字字符组成一个整数。

例如:a.txt中的内容是1j3j5sd0msd454ss3msd563,则抽取出来写入文件b.txt的内容应该是135 45 435 63(第二个整数是由045三个数字组成,所以是45),要修改的代码如下...








#include <stdio.h>

// 函数extractDigit的功能:从文件a.txt中提取数值写入文件b.txt中
void extractDigit();

// 请在此添加代码,实现extractDigit函数
// 函数extractDigit的功能:从文件a.txt中提取数值写入文件b.txt中
void extractDigit()
{
    FILE *fi = fopen("a.txt","r");  // 以读的方式打开文件a.txt
    FILE *fo = fopen("b.txt","w");  // 以写的方式打开文件b.txt
    if(fi==NULL || fo==NULL)  // 如果某个文件打开失败,则返回
        return;
    /**************Begin******************/
    char c;
    while((c=fgetc(fi))!=EOF)
    {
        if(c>'1'&&c<'9')
        {
        fputc(c,fo);
        }
    }
    /**************End********************/
    fclose(fi);  // 关闭文件fi
    fclose(fo);  // 关闭文件fo
}


新手小白太难了,我知道这种题对大佬来说没什么难度,但求指教。
最佳答案
2020-11-30 16:59:50
void extractDigit()
{
        FILE *fi = fopen("a.txt", "r");
        FILE *fo = fopen("b.txt", "w");  // 以写的方式打开文件b.txt
        if (fi == NULL || fo == NULL)  // 如果某个文件打开失败,则返回
                return;
        /**************Begin******************/
        char c;
        int i = 0;
        while ((c = fgetc(fi)) != EOF)
        {
                if (c >= '0'&&c <= '9')
                {
                        i++;
                        if (c != '0')  fputc(c, fo);                       
                        if ((i % 3) == 0) fputc(' ', fo);                                       
                }               
        }
        /**************End********************/
        fclose(fi);  // 关闭文件fi
        fclose(fo);  // 关闭文件fo
}

最佳答案

查看完整内容

void extractDigit() { FILE *fi = fopen("a.txt", "r"); FILE *fo = fopen("b.txt", "w"); // 以写的方式打开文件b.txt if (fi == NULL || fo == NULL) // 如果某个文件打开失败,则返回 return; /**************Begin******************/ char c; int i = 0; while ((c = fgetc(fi)) != EOF) { if (c >= '0'&&c
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-30 16:59:50 | 显示全部楼层    本楼为最佳答案   
void extractDigit()
{
        FILE *fi = fopen("a.txt", "r");
        FILE *fo = fopen("b.txt", "w");  // 以写的方式打开文件b.txt
        if (fi == NULL || fo == NULL)  // 如果某个文件打开失败,则返回
                return;
        /**************Begin******************/
        char c;
        int i = 0;
        while ((c = fgetc(fi)) != EOF)
        {
                if (c >= '0'&&c <= '9')
                {
                        i++;
                        if (c != '0')  fputc(c, fo);                       
                        if ((i % 3) == 0) fputc(' ', fo);                                       
                }               
        }
        /**************End********************/
        fclose(fi);  // 关闭文件fi
        fclose(fo);  // 关闭文件fo
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-30 17:39:42 | 显示全部楼层
北冰羊 发表于 2020-11-30 17:25
void extractDigit()
{
        FILE *fi = fopen("a.txt", "r");

对不起,这个不对,还不能删,。。。略过,略过
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-30 21:53:00 | 显示全部楼层
北冰羊 发表于 2020-11-30 17:25
void extractDigit()
{
        FILE *fi = fopen("a.txt", "r");

大佬你这里差一点啊,比如测试输入:
q12h44h67k854ksd923823sf83wd38457sd433ds,
预期输出:
124 467 854 923 823 833 845 743 3
但你这个输出不了最后的3了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-30 22:03:19 | 显示全部楼层
北冰羊 发表于 2020-11-30 17:39
对不起,这个不对,还不能删,。。。略过,略过

可以修改原来的帖子哦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-1 21:38:59 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. void main(){
  4.         FILE *fi = fopen("a.txt","r");  // 以读的方式打开文件a.txt
  5.     FILE *fo = fopen("b.txt","w");  // 以写的方式打开文件b.txt
  6.         char c='\0';
  7.         int i=0;
  8.         while(!feof(fi)){
  9.                 c=fgetc(fi);
  10.                 if(0!=isdigit(c)){
  11.                         if(i==3){
  12.                                 fputc(' ',fo);
  13.                                 i=0;
  14.                         }
  15.                         if((i!=0)||(c!='0'))fputc(c,fo);
  16.                         i++;
  17.                 }
  18.         }
  19.         fclose(fi);
  20.         fclose(fo);
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-1 22:00:00 | 显示全部楼层
北冰羊 发表于 2020-11-30 17:25
void extractDigit()
{
        FILE *fi = fopen("a.txt", "r");

好吧兄弟是我们的平测系统有问题,你的答案对了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-8 11:51:20 | 显示全部楼层
根据上面的答案修改了以下,而且我做的实训要求测试输入:
1212121212343434343445454545450000005656565665dfsd455
预期输出:

121 212 121 234 343 434 344 545 454 545
0 0 565 656 566 545 5


  1. void extractDigit()
  2. {
  3.         FILE *fi = fopen("a.txt", "r");
  4.         FILE *fo = fopen("b.txt", "w");  // 以写的方式打开文件b.txt
  5.         if (fi == NULL || fo == NULL)  // 如果某个文件打开失败,则返回
  6.                 return;
  7.         /**************Begin******************/
  8.         char c,s,a;
  9.         int i = 0;
  10.         while (!feof(fi))
  11.         {
  12.                 c=fgetc(fi);
  13.                 if(c=='0' && i!=0)
  14.                     i++;
  15.                 if(c=='0' && i==0)
  16.                 {
  17.                     c=fgetc(fi);
  18.                     i++;
  19.                     if(c=='0')
  20.                     {
  21.                         i++;
  22.                         c=fgetc(fi);
  23.                         if(c=='0')
  24.                         {
  25.                             fputc('0',fo);
  26.                             i++;
  27.                         }
  28.                     }
  29.                 }
  30.                 if (c >'0'&&c <= '9')
  31.                 {
  32.                     fputc(c,fo);                  
  33.                     i++;                     
  34.                 }
  35.                 if(i==3)
  36.                 {
  37.                     fputc(' ',fo);
  38.                     i=0;
  39.                 }           
  40.         }
  41.         fputc(' ',fo);
  42.         /**************End********************/
  43.         fclose(fi);  // 关闭文件fi
  44.         fclose(fo);  // 关闭文件fo
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 16:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表