Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用系统函数运行另一个.cpp文件_C_Text Files_Main_Src - Fatal编程技术网

使用系统函数运行另一个.cpp文件

使用系统函数运行另一个.cpp文件,c,text-files,main,src,C,Text Files,Main,Src,这个程序是一个“分级器”程序,我只要求用户输入一个txt文件的名称和一个.cpp源文件,该文件处理txt文件并获取其信息。然后,我将源文件与txt文件一起编译,txt文件输出另一个文本文件。然后将这种新纺织品与预期产量进行比较(我也得到了) 系统函数允许用户从C程序运行UNIX命令。当我试图编译用户提供的源文件时 我这样说是有错误的 “_main”,引用自:主可执行文件的隐式输入/启动。 叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用) sh:./myProg:没有这样的文件或目录

这个程序是一个“分级器”程序,我只要求用户输入一个txt文件的名称和一个.cpp源文件,该文件处理txt文件并获取其信息。然后,我将源文件与txt文件一起编译,txt文件输出另一个文本文件。然后将这种新纺织品与预期产量进行比较(我也得到了)

系统函数允许用户从C程序运行UNIX命令。当我试图编译用户提供的源文件时

我这样说是有错误的

“_main”,引用自:主可执行文件的隐式输入/启动。
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)
sh:./myProg:没有这样的文件或目录

我的教授提供的我正在编译的源文件有一个函数,如下所示:

#include <stdio.h>
#include <stdlib.h>
#define  MAX_VALUES    3
#define  OUTPUT_LINES   5

int notmain(int argc, char **argv)
{
/*
 * argv is just the file name

 */
//printf(argv[1]);
int values[MAX_VALUES];
int i, j;


FILE *inputFile;
char name [20]="input.txt"; // I have included this piece of code to see if there is a correct output from the source file provided by the user. 
if ( (inputFile = fopen(name, "r") ) == NULL) {
     printf("Error opening input file.\n\n");
     exit(1);
}
for(i = 0; i < MAX_VALUES; i++)
    fscanf(inputFile, "%d", &values[i]);
for(i = 0; i < OUTPUT_LINES; i++){
   for (j=0; j < MAX_VALUES; j++)
      printf("%d ", values[j]*(i+1) + j);
   printf("\n");
}
return 0;
}
#包括
#包括
#定义最大值3
#定义输出_行5
int notmain(int argc,字符**argv)
{
/*
*argv只是文件名
*/
//printf(argv[1]);
int值[最大值];
int i,j;
文件*输入文件;
char name[20]=“input.txt”;//我包含了这段代码,以查看用户提供的源文件是否有正确的输出。
if((inputFile=fopen(名称,“r”))==NULL){
printf(“打开输入文件时出错。\n\n”);
出口(1);
}
对于(i=0;i
我写的代码如下:这段代码从用户那里获取信息,然后进行编译

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_LINES 5

int main(){
    char srcfile[200];
    char inpfile[200];
    char resultfile[200];

    printf("Please enter the name of the source file: \n");
    scanf("%s",srcfile);
    printf("Please enter the name of the input file: \n");
    scanf("%s",inpfile);
    printf("Please enter the name of the expected result file: \n");
    scanf("%s",resultfile);

    char test1 [100]="gcc -o myProg ";
    char test2 [100]="./myProg ";

    strcat(test2,inpfile);
    strcat(test2," > ");
    strcat(test2,resultfile);
    strcat(test1,srcfile);
    printf("%s\n",test1); //these are just tests 
    printf("%s",test2);  //these are just tests

    if (system(test1)) {
        printf("There is an error compiling the program  ");
    } 

    if (system(test2)!= 0) {
        printf("There is an error running the executable");
    } 

    return 0;
}
#包括
#包括
#包括
#定义第5行的数量
int main(){
char srcfile[200];
字符输入文件[200];
char结果文件[200];
printf(“请输入源文件的名称:\n”);
scanf(“%s”,srcfile);
printf(“请输入输入文件的名称:\n”);
scanf(“%s”,inpfile);
printf(“请输入预期结果文件的名称:\n”);
scanf(“%s”,结果文件);
char test1[100]=“gcc-o myProg”;
char test2[100]=“/myProg”;
strcat(测试2,输入文件);
strcat(测试2,“>”);
strcat(test2,resultfile);
strcat(test1,src文件);
printf(“%s\n”,test1);//这些只是测试
printf(“%s”,test2);//这些只是测试
if(系统(测试1)){
printf(“编译程序时出错”);
} 
如果(系统(测试2)!=0){
printf(“运行可执行文件时出错”);
} 
返回0;
}

如果您正在寻找解决方案,我已将其发布在答案中,您尝试编译的文件没有
main
函数,这是C程序的入口点。这意味着实际上不可能单独将该文件构建到可执行文件中

如果函数名应为
notmain
,则必须编写另一个源文件,该文件具有
main
函数并调用
notmain
。第二个
main
将属于您的程序正在编译的可执行文件,而不是您的程序。您将有三个源文件:

  • 处理编译的分级程序
  • 一种包装文件,有效地执行以下操作:

    int main(int argc, char *argv[]) {
        notmain(argc, argv);
    }
    
  • 最后是要评分的课程

您还需要使用
extern
函数
notmain
或提供一个头来共享它。然后,你的分级程序将编译包装器
main
和要一起分级的源文件。

问题:你能用两个主要函数运行两个c程序吗?答案是肯定的。为了做到这一点,您必须使用终端分别编译具有两个主要功能的程序。然而,如果他们相互影响,恐怕我现在没有一个解决方案,在这个具体的案例中,我就是这样做的。我到终点站写信。在本例中,我运行一个程序,该程序使用系统函数运行另一个程序

然后,我编写了gcc-omyprogrammain.o 这将创建一个名为Myprogram的可执行文件,您可以通过编写

 ./Myprogram 

在本例中,我的主要方法是编译另一个源文件,因此我不需要在终端中也编译该程序。当我编译这个程序时,它在可执行文件和源文件所在的同一目录中创建了一个output.txt文件

您应该编辑上一个问题,使其包含所需信息,而不是发布新问题。@VarunRao如果您遵循这样的惯例,您更有可能获得快速帮助。当我看到“我不关心堆栈溢出规则,只需解决我的问题”时,我停止了阅读。不,这些不是确切的话,但这是我收到的。如果你不尊重网站规则,你很可能很快就无法提出进一步的问题。你的教授提供的文件不是一个独立的程序。你的教授到底希望你做什么?我不能在一个项目@cormac中有两种主要方法-obrien@VarunRao尝试将
notmain()
更改为
main()
,并测试其是否“有效”。然后我们可以解决“我不能在一个项目中有两个主要方法”的限制。我在几个小时前就已经测试过了,有两个主要方法不起作用,除非你分别编译它们@chux@VarunRao“我的教授提供的源文件”不需要是(您)编写的代码的一部分“我写过”。您的程序运行编译教授的代码。@VarunRao:C不支持方法,只支持函数
 ./Myprogram