顺便说一句,我知道这是重复的,但我找不到答案!
我正在尝试制作一个编译并运行另一个C ++文件的C ++文件,并检查输出是否正确或不正确(我知道这有点奇怪)。
(Desktop文件夹有一个eval.cpp和一个a.out +一个“test”文件夹 test文件夹有main.cpp,tytytyt.in,tytytyt.out,tytytyt.ok)
eval.cpp
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
using namespace std;
ifstream fa("~/Desktop/test/tytytyt.out");
ifstream fok("~/Desktop/test/tytytyt.ok");
string filename;
int a, ok;
int main()
{
cout << "In ce folder se afla fisierul pe care ai dori sa testezi? \n";
cin >> filename;
string str = "g++ -o a.out" + filename;
const char * command = str.c_str();
cout << "Compilare sursa cu ajutorul comenzii " << command << " ... \n";
system(command);
cout << "Rulare fisier... \n";
system("~/Desktop/test/a.out");
fa >> a;
fok >> ok;
if(a == ok) cout << "Corect!";
else cout << "Incorect!";
return 0;
}
当我运行~/Desktop$ g++ eval.cpp
时,它会创建一个a.out
文件。
然后我运行~/Desktop$ ./a.out
。
当我的程序打印In ce folder se afla fisierul pe care ai dori sa testezi?
并编写~/Desktop/test/main.cpp
时,程序会打印出来
Compilare sursa cu ajutorul comenzii g++ ~/Desktop/test/main.cpp ...
Rulare fisier...
然后是错误:sh: 1: /home/steven/Desktop/test/a.out: not found
如果我检查测试文件夹,文件a.out
不存在。
有人能帮我吗? `
有
string str = "g++ -o a.out" + filename; const char * command = str.c_str(); cout << "Compilare sursa cu ajutorul comenzii " << command << " ... \n"; system(command);
如果编译成功a.out在当前目录中生成,因为您没有指定必须在哪里生成它
如果你想生成~/Desktop/test/a.out
替换
string str =“g ++ -o a.out”+ filename;
通过
string str = "g++ -o ~/Desktop/test/a.out" + filename;
除了我不明白编译是如何完成的,因为a.out
之后没有空格用文件名分隔,文件名是通过cin >> filename;
设置的,所以它是一个单词,不能用空格或其他分隔符开头,你确定吗?编译完成了吗?你甚至没有测试系统的结果......
对我来说一定是
string str = "g++ -o ~/Desktop/test/a.out " + filename;