pea*_*ear 4 c++ input command-line-arguments
这是我的代码 - 不知道为什么我收到此错误消息:
$ ./main.cpp "hello" "is"
./main.cpp: line 4: syntax error near unexpected token `('
./main.cpp: line 4: `int main(int argc, char *argv[]){'
Run Code Online (Sandbox Code Playgroud)
它在 g++ 中编译得很好,但是当我运行它时,出现上述错误。知道为什么吗?这是我的完整代码..
#include <iostream>
#include <fstream>
int main(int argc, char *argv[]){
for(int i = 0; i < argc; i++){
std::cout << argc << " : " << argv[i] << '\n';
}
if (argc != 2){
std::cout << "\nUSAGE: 2 command line arguments please." << std::endl;
std::cout << "\n (1) Input file with raw event scores.\n (2) Output file to write into.";
}
// open the font file for reading
std::string in_file = argv[1];
std::ifstream istr(in_file.c_str());
if (!istr) {
std::cerr << "ERROR: Cannot open input file " << in_file << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您必须运行编译后的程序,而不是源代码:
\n\n$ g++ -o main main.cpp\n$ ./main "hello" "is"\n3 : ./main\n3 : hello\n3 : is\n\nUSAGE: 2 command line arguments please.\n\n\xc2\xa0 \xc2\xa0(1) Input file with raw event scores.\n\xc2\xa0 \xc2\xa0(2) Output file to write into.ERROR: Cannot open input file hello\nRun Code Online (Sandbox Code Playgroud)\n\n您的示例尝试将 C++ 代码作为 shell 脚本执行,但这是行不通的。正如您从我的程序测试运行的输出中看到的,您仍然有一些错误需要解决。
\n