【问题标题】:read a text file, make some trivial transformation character by character (swapping the case of all letters), write result to text file读取文本文件,逐个字符进行一些简单的转换(交换所有字母的大小写),将结果写入文本文件
【发布时间】:2021-02-21 02:33:27
【问题描述】:

我必须读取一个文本文件,逐个字符进行一些简单的转换(交换所有字母的大小写),然后将结果写入文本文件。我写了这段代码,但它不起作用。请在这方面指导我。提前感谢您

#include <stdio.h>
#include <stdlib.h> 
int main() {
    char c[1000];
     char x[100];
     char var;
     int i;
    FILE *fptr;
    if ((fptr = fopen("text.txt", "r")) == NULL) {
        printf("Error! opening file");
        // Program exits if file pointer returns NULL...
        exit(1);
    }

    // reads text until a newline is encountered...
    fscanf(fptr, "%[^\n]", c);
    printf("Data from the file:\n%s", c);
    // Convert the file to upper case....
     
    for( i=0;i<= strlen(c);i++){
      if(c[i]>=65&&c[i]<=90)
         c[i]=c[i]+32;
   }
      fptr = fopen("program.txt","w");
      fprintf(fptr,"%[^\n]",c);
     
      fclose(fptr);
    return 0;
}
    

【问题讨论】:

    标签: c string file


    【解决方案1】:

    编辑:添加 #include &lt;stdlib.h&gt;,删除 static 描述 main()

    我的建议,基于复制我大学给出的文件的示例。

    我使用了 ctype.h 中的 toupper(),如果你不想使用它,你可以在与你的解决方案类似的条件下添加 32

    注意:可能有char c 而不是int c。 (在原始版本中,它实际上是char;我更改了它,因为如果您查看处理c 的所有函数的文档中的标题,它们都采用/返回int,而不是char;在您的版本更重要,因为你保留一个数组,在我的程序中它几乎没有任何变化——int 只是我的首选做法)。

    注2:我实际上从未深入研究过“w”/“r”(写入/读取)和“wb”/“rb”(写入/读取二进制)之间的区别。代码似乎可以正常工作。

    (我认为当文件是文本文件时没有太大区别,为了进一步确保两个版本都能正常工作,请注意代码使用 feof() 来处理 EOF)

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void) {
        FILE *from, *to;
        int c;//could be char
        
        /* opening the source file */
        if ((from = fopen("text.txt", "rb")) == NULL) {
            printf("no such source file\n");
            exit(1);
        }
        
        /* opening the target file */
        if ((to = fopen("program.txt", "wb")) == NULL) {
            printf("error while opening target file\n");
            exit(1);
        }
        
        while (!feof(from)) {
            c = fgetc(from);
            
            if (ferror(from)) {
                printf("error while reading from the source file\n");
                exit(1);
            }
            
            if (!feof(from)) {//we avoid writing EOF
                fputc(toupper(c), to);
                if (ferror(to)) {
                    printf("error while writing to the target file\n");
                    exit(1);
                }
            }
        }
        
        if (fclose(from) == EOF) {
            printf("error while closing...\n");
            exit(1);
        }
        
        
        if (fclose(to) == EOF) {
            printf("error while closing...\n");
            exit(1);
        }
    
        return 0;
    }
    

    对于从命令行获取参数的版本(也适用于 Windows)将 main 的开头替换为

    int main(int argc, char *argv[]) {
        FILE *from, *to;
        char c;
    
        /* checking the number of arguments in the command line */
        if (argc != 3) {
            printf("usage: name_of_executable_of_this_main <f1> <f2>\n");//name_of_exe could be copy_to_upper, for example; change adequately
            exit(1);
        }
        
        /* opening the source file */
        if ((from = fopen(argv[1], "rb")) == NULL) {
            printf("no such source file\n");
            exit(1);
        }
        
        /* opening the target file */
        if ((to = fopen(argv[2], "wb")) == NULL) {
            printf("error while opening the target file\n");
            exit(1);
        }
        
    

    【讨论】:

    • 有趣的是,我的“运动老师”教我们使用int,教授在讲座中使用char; char 的主要问题是,(char)-1 可以是有效字符,而(int)-1 是 EOF。在这里没关系:我使用feof()(按照教授的建议),所以(c == EOF) 没有猴子生意,我很确定无论如何都不能与“rb”一起使用
    • int c;//could be char?!?不,它不能char[f]getc() 返回 int 而不是 char,因为 EOF 不适合 char
    • 它在原始版本中是 char,它工作得很好,因为它应该 - 请注意,我不会检查结果 [f]getc() 是否在任何地方都是 EOF。不过我同意,最好使用 int
    • 在linux上查过,连gcc -Wall -Wextra都没说一句话。好吧,我希望任何阅读我的答案的人都可以看到,使用 char 不是最好的,并且 使用 feof() 很好 所以你真的不需要太担心了。
    【解决方案2】:

    我不知道如何用那种语言编写代码(我认为它是 C++),但基本上你应该做的是一个 for 循环来遍历字符串中的每个字符。在 Python 中它看起来像:

    x = open("text.txt", "r")
    y = open("new text.txt","w")
    z = ""
    for char in x:
        z += char.upper()
        
    y.write(z)
    

    我希望我能够提供一个关于如何解决您的问题的想法。我也是一个新手,但是在 Python 中。

    【讨论】:

    • 先生,我用 C 语言编写了这段代码,它的要求是 1_ 读取文本文件 2_ 读取文件后更改字母的大小写 3_ 然后将结果保存到文件中。但我的代码只是读取文件并在写入部分保存括号。现在,如果您对此有任何想法,请告诉我。
    • 将文件打开到一个变量,使用for循环遍历变量中的每个字符并将它们转换为大写,然后将转换后的变量写入一个空白文本文件。
    • 不要使用幻数。 32和65不是你的朋友。 islowertoupper 是。
    • 从不,EVER 使用 (f)scanf 读取一行(或其他任何内容)。使用fgets检查返回值,处理,使用fputs检查返回值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多