【问题标题】:How to execute C program in Linux or Windows simple commands to print a file如何在 Linux 或 Windows 中执行 C 程序打印文件的简单命令
【发布时间】:2021-08-24 15:31:14
【问题描述】:
  1. 您好,我正在尝试制作一个可以打印自己源代码的 C 程序。

  2. 我假设源代码文件和执行文件在同一个目录下,没有任何其他.c文件。

  3. 好吧,我尝试了很多代码,但都没有成功。

  4. 错误原因是因为命令的顺序(如果是windows命令先传入windows操作系统,如果是linux命令先传入linux操作系统)。

  5. 我试图忽略 fopen 和其他功能,只使用简单的操作系统命令。

  6. 这是我尝试过的代码:

    在 Windows 上工作的代码:


/* C library statement */

#include <stdlib.h>

/* main program */

int main()
{/* start of main */

    int windows = 0; /* a variable to check if we are running windows or unix operation system */
/* system function returns -1 value if it failed.
 * so if unix value is -1 we are not running unix system.
 * if we are not running unix system we will execute windows commands.
 */

    windows = system("type *.c"); /* windows command to print file text */
    if(windows == -1) /* if windows command failed (and the specific file is exists) then we are not running windows operation system */
        system("cat *.c"); /* unix command to print file text */
    return 0;

}/* end of main */

在 linux 上工作的代码:

/* this program will print herself source code by using simple linux or windows commands */

#include <stdlib.h>

/* main program */

int main()
{/* start of main */

    int unix = 0; /* a variable to check if we are running windows or unix operation system */
/* system function returns -1 value if it failed.
 * so if unix value is -1 we are not running unix system.
 * if we are not running unix system we will execute windows commands.
 */

    unix = system("cat *.c"); /* unix command to print file text */
    if(unix == -1) /* if unix command failed (and the specific file is exists) then we are not running unix operation system */
        system("type *.c"); /* windows command to print file text */
    return 0;

}/* end of main */

问题是 linux 将 type 识别为 bash 命令,而 windows 没有编译 cat 命令

【问题讨论】:

  • 使用标准文件 IO 函数使程序可移植(fopenfread 和朋友)
  • #1 中描述的程序类型的术语是“quine”。
  • 您应该发布有效的代码作为答案,而不是编辑到问题帖子中。
  • @Armali 抱歉,我会解决这个问题,我是这个论坛的新手。

标签: c linux windows cmd


【解决方案1】:

我使这段代码工作,可以在两个操作系统中编译和运行

#include <stdlib.h>

/* defining variables to check if what OS we are running in */

#if defined (_WIN32) || defined (_WIN64) /* if we are running win 32bit or win 64 bit */
    #define HAVEWIN 1 /* defined we are running at windows OS */
    #define HAVEUNIX 0 /* defined we are not running at unix OS */
#elif defined (__unix__) /* if we are running a unix OS */
    #define HAVEUNIX 1 /* defined we are running at unix OS */
    #define HAVEWIN 0 /* defined we are not running at unix OS */
#endif /* end of the OS checks */

/* main program */

int main()
{/* start of main */

    if(HAVEWIN == 1) /* if we are running on windows OS */
        system("type *.c"); /* use windows command to print file text */
    else if(HAVEUNIX == 1) /* if we are running on unix OS */
        system("cat *.c"); /* use unix command to print file text */
    return 0;

}/* end of main */

【讨论】:

  • 对于特定于平台的代码,您应该使用#if 而不是if
【解决方案2】:

符号__FILE__ 始终扩展为当前文件的名称。以下代码将打印源代码位置:

#include <stdio.h>

int main()
{
  printf("%s\n", __FILE__);
  return 0;
}

所以你应该只添加代码来转储其内容。 但是不要使用system() 调用,因为它不便携而且效率很低。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2013-09-04
    相关资源
    最近更新 更多