【问题标题】:Save to a text file what was wrote at the console. C将在控制台中写入的内容保存到文本文件中。 C
【发布时间】:2014-08-11 18:21:49
【问题描述】:

我写了一些代码来编辑文本文件。当程序执行时,控制台写信息让用户知道发生了什么。我的问题是:如何将用户和程序在控制台写入的所有信息保存到文本文件中?

谢谢

【问题讨论】:

    标签: c file text console


    【解决方案1】:

    一般情况下,您可以将stdout 重定向到带有> 字符的文件:

    your_program_name whatever arguments here > target.file
                                              ^^^^^^^^^^^^^
    

    【讨论】:

      【解决方案2】:

      我已经多年没有使用 C++,但这是使用 fopen() 和 fclose() 写入和读取文件的一个简单示例。对这个函数做一点研究。

      #include <stdio.h>
      
      int main (void) {
      
          char userInput[100];
          int res;
      
          FILE *userFile; //Pointer to FILE struct
      
          //Open file
          if ( ( userFile = fopen("userFile.txt", "r+") ) != NULL ) {
      
              //Read user Input
              gets( userInput );
              //Writes userInput content to File
              fputs( userInput, userFile );
              //Closes file
              fclose(userFile);
          }
          else
              printf ("Can't open file");
      
          return 0;
      }
      

      【讨论】:

      • 你的意思是I've not used C in years
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多