首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用C语言从文件输出文本

用C语言从文件输出文本
EN

Stack Overflow用户
提问于 2022-11-27 18:17:57
回答 1查看 40关注 0票数 -2

我试图在C上编写一个代码,它读取file.txt,将其输出到控制台,然后计数行、单词等,毕竟是将file.txt的内容导出到file2.txt,但顺序相反。

案文需要从以下几个方面着手:

我爱你

对此:

ouY evoL I

我的text.file:在这里输入图像描述

现在我的代码得到了什么:在这里输入图像描述

这里是我需要改进的代码,因为它打印了我需要的代码,但是带有空行,这是不需要的。它还需要导出到另一个文件中:

代码语言:javascript
运行
复制
        fseek(fptr,0,SEEK_END);
        pos=ftell(fptr);
        i=0;
        while(i<pos)
            {
            i++;
            fseek(fptr,-i,SEEK_END);
            ch=fgetc(fptr);
            printf("%c",ch);
            }

有完整的代码:

代码语言:javascript
运行
复制
#include <stdio.h>

int main ()
{
    FILE *fptr;
    int i, n, j, pos;
    char str[100];
    char fname[20]="mfile.txt";
    char newch[500];
    int wrd=1,charctr=1,rows=1;
    char str1;
    char ch;
    int no_lines = 1;
    int COUNT = 0;


    fptr = fopen(fname,"r"); 
    if(fptr == NULL) 
     { 
        printf(" \n");
        printf("File does not exist or can not be opened."); 
      } 
    else 
        { 
          ch=fgetc(fptr); 
          printf(" \n");
          printf("The content of the file %s are: \n", fname);
          printf(" \n"); 
          while(ch != EOF) 
            { 
                printf("%c",ch); 
                if(ch==' '||ch=='\n')
                    {
                        wrd++; 
                    }
                    else
                    {
                        charctr++; 
                    }
                if(ch=='\n')
                    {
                        rows++;
                    }
                ch=fgetc(fptr); 
            }

        int wrd1 = wrd - 1;
        float charctr1 = charctr - 1;
        float rows1 = rows;
        float averageSymbol = charctr1 / rows1;

        printf(" \n");
        printf("\nwrd = %d, charctr = %d", wrd, charctr-1);
        printf("\nThe number of rows in the file %s are : %d\n", fname,rows);
        printf("\nThe average amount of symbols in a row is %f\n", averageSymbol);
        printf(" \n");

        }

        fseek(fptr,0,SEEK_END);
        pos=ftell(fptr);
        i=0;
        while(i<pos)
            {
            i++;
            fseek(fptr,-i,SEEK_END);
            ch=fgetc(fptr);
            printf("%c",ch);
            }

    fclose(fptr); 
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-27 18:50:11

好的,如果我正确理解了,您想要去掉代码中显示的一些空行,然后还要将程序的输出(反向文本)写入另一个文件中?我假设你希望句子也按原顺序排列。

代码语言:javascript
运行
复制
#include <stdio.h>

int main ()
{
    FILE *fptr;
    FILE *fptr2;
    int i, n, j, pos;
    char str[100];
    char fname[20]="mfile.txt";
    char fname2[20]="outputfile.txt";
    char newch[500];
    int wrd=1,charctr=1,rows=1;
    char str1;
    char ch;
    int no_lines = 1;
    int COUNT = 0;
    fptr2 = fopen(fname2, "w+");
    
    fptr = fopen(fname,"r"); 
    if(fptr == NULL) 
     { 
        printf(" \n");
        printf("File does not exist or can not be opened."); 
      } 
    else 
        { 
          ch=fgetc(fptr); 
          
          printf("The content of the file %s are: \n", fname);
          
          while(ch != EOF) 
            { 
                printf("%c",ch); 
                if(ch==' '||ch=='\n')
                    {
                        wrd++; 
                    }
                    else
                    {
                        charctr++; 
                    }
                if(ch=='\n')
                    {
                        rows++;
                    }
                ch=fgetc(fptr); 
            }

        int wrd1 = wrd - 1;
        float charctr1 = charctr - 1;
        float rows1 = rows;
        float averageSymbol = charctr1 / rows1;

        
        printf("\nwrd = %d, charctr = %d", wrd, charctr-1);
        printf("\nThe number of rows in the file %s are : %d\n", fname,rows);
        printf("The average amount of symbols in a row is %f\n", averageSymbol);
        

        }

        fseek(fptr,0,SEEK_END);
        pos=ftell(fptr);
        i=0;
        while(i<pos)
            {
            i++;
            fseek(fptr,-i,SEEK_END);
            ch=fgetc(fptr);
            printf("%c",ch);
            fputc(ch,fptr2);
            }

    fclose(fptr); 
    fclose(fptr2); 
    return 0;
}

这个版本首先检查一个名为outputfile.txt的文件是否存在,如果不存在,它将创建它。如果所述文件已经存在,则该程序用新的程序输出覆盖原来在文件中的内容(它不会一直清除该文件)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74592613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档