简体   繁体   English

如何在 Linux 或 Windows 中执行 C 程序打印文件的简单命令

[英]How to execute C program in Linux or Windows simple commands to print a file

  1. Hi there, I am trying to make a C program that will print herself source code.您好,我正在尝试制作一个 C 程序来打印自己的源代码。

  2. I am assuming that the source code file and the execute file are in the same directory without any other.c files.我假设源代码文件和执行文件在同一个目录中,没有任何其他.c文件。

  3. Well I have tried many codes, and it didn't worked out.好吧,我尝试了很多代码,但都没有成功。

  4. The error cause because the order of the commands (if it is windows command first it passed in windows OS and if it is a linux command first it passed in linux OS). The error cause because the order of the commands (if it is windows command first it passed in windows OS and if it is a linux command first it passed in linux OS).

  5. I am trying to ignore fopen and other functions and to use only simple OS commands.我试图忽略 fopen 和其他功能,只使用简单的操作系统命令。

  6. Here is the code I have tried:这是我尝试过的代码:

    code working on windows:在 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 */

code working on linux:在 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 */

well the problem is that linux recognize type as a bash command and windows is not compiling the cat command问题是 linux 将类型识别为 bash 命令和 windows 没有编译 cat 命令

I Made This Code Working, Can Be Compiled and Run in Both OS我使这段代码工作,可以在两个操作系统中编译和运行

#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 */

The symbol __FILE__ always expands to the name of the current file.符号__FILE__始终扩展为当前文件的名称。 The following code will print the source code location:以下代码将打印源代码位置:

#include <stdio.h>

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

So you should just then just add code to dump its contents.所以你应该只是添加代码来转储它的内容。 But don't use system() calls, as this is not portable and very inefficient.但是不要使用system()调用,因为这不是可移植的而且效率很低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM