首页 > 解决方案 > 3.3 C中读取的字符串不能倒序输出

问题描述

我想以相反的顺序将从文件 data01.in 读取的字符串写入 data03.out。但是,当我运行它时,两个文件具有相同的内容。我应该怎么办?之后,打开写入你创建的输出结果的文件和模型文件检查是否匹配,从头开始逐个字符比较,检查两个文件的内容是否匹配。我想找出它是什么并创建一个显示结果的程序,所以请告诉我如何创建该程序。

资源

    #include <stdio.h>
        #include <stdlib.h>
    
    
        #define MAX 1000
         
        int main ()
        {
          char in_filename [] = "data01.in";
          
          char out_filename [] = "data03.out"
    ;
          FILE * fpin, * fpout;
         
          char buf [MAX];
          int c;
          int n;
          int i;
         
          / * Open the input file. * /
          if ((fpin = fopen (in_filename, "r")) == NULL)
          {
            printf ("Failed to open:% s \ n", in_filename);
            exit (1);
          }
         
          / * Open the output file. * /
          if ((fpout = fopen (out_filename, "w")) == NULL)
          {
            printf ("Failed to open:% s \ n", out_filename);
            fclose (fpin);
            exit (2);
          }
         
         
          / * Read data up to the maximum number of buffers * /
          for (n = 0; n <MAX; ++ n)
          {
            if ((c = fgetc (fpin)) == EOF)
              break;
         
            buf [n] = (char) c; / * Don't forget to cast to char. * /
          }
         
          / * If EOF is not reached even after the maximum number of buffers is reached, an error is processed as buffer overflow.
          * /
          if (n == MAX)
          {
            printf ("Buffer overflow! \ N");
            exit (3);
          }
         
          / * Output the contents of the buffer. * /
          for (i = 0; i <n; ++ i)
          { 
            fputc (buf [i], fpout);
          }
         
          / * Don't forget to close each opened I / O file. * /
          fclose (fpout);
          fclose (fpin);
         
          return 0;
        }

数据01.in

    A Turing machine is a mathematical model of computation that defines an abstract machine,[1] which manipulates symbols on a strip of tape according to a table of rules.[2]
    Despite the model's simplicity, given any computer algorithm, a Turing machine capable of simulating that algorithm's logic can be constructed.[3]
    "Turing machine" from Wikipedia(https://en.wikipedia.org/wiki/Turing_machine). (Access date: 2020/10/01)

数据03.out

    )10/01/0202 :etad sseccA( .)enihcam_gniruT/ikiw/gro.aidepikiw.ne//:sptth(aidepikiW morf "enihcam gniruT"
    ]3[.detcurtsnoc eb nac cigol s'mhtirogla taht gnitalumis fo elbapac enihcam gniruT a ,mhtirogla retupmoc yna nevig ,yticilpmis s'ledom eht etipseD
    ]2[.selur fo elbat a ot gnidrocca epat fo pirts a no slobmys setalupinam hcihw ]1[,enihcam tcartsba na senifed taht noitatupmoc fo ledom lacitamehtam a si enihcam gniruT A

标签: c

解决方案


推荐阅读