Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
如果ARCC=2,C++创建文件?_C++_File_Command Line Arguments_Argc - Fatal编程技术网

如果ARCC=2,C++创建文件?

如果ARCC=2,C++创建文件?,c++,file,command-line-arguments,argc,C++,File,Command Line Arguments,Argc,我真的不知道这里出了什么问题。。。 我希望代码在终端中键入一个参数时创建一个文件。如果argc设置为1,则可以正常工作,但不能高于该值。 非常感谢您的帮助! 干杯&一个美好的星期天 #include <fstream> #include <string> #include <iostream> using namespace std; int main(int argc, char* argv[]) { if (argc==(2)) {

我真的不知道这里出了什么问题。。。 我希望代码在终端中键入一个参数时创建一个文件。如果argc设置为1,则可以正常工作,但不能高于该值。 非常感谢您的帮助! 干杯&一个美好的星期天

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    if (argc==(2))
    {
        ofstream file("MyFile.txt"); // Creates MyFile.txt
    }

    else
    {
        cout << "type one argument !" << endl;
    }
    return 0;
}

如果您使用的是Visual Studio,那么答案是:转到“项目/属性”,选择“调试”选项卡,并在“命令参数”字段中输入参数。

请显示您正在使用的命令行输入。这看起来是正确的。你试过调试吗?设置断点并查看argc和argv值是什么。在将argc接收的值与2的值进行比较之前,了解argc接收的值会很有帮助。在if argc==2行中是否有cout语句或调试器的值?在终端中,我只需键入类似于/code.exe a的内容,就像在程序名后的第二个参数“a”中一样。通过调试检查,当我做一个cout时,使用了什么编译器版本和开发系统设置?