Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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
C++ 输入文件名作为命令_C++ - Fatal编程技术网

C++ 输入文件名作为命令

C++ 输入文件名作为命令,c++,C++,我必须通过编程从输入文本文件中读取一些值 int main(){ FILE *pf; int i; int j; pf = fopen("input.txt" , "r"); fscanf(pf ,"%d , %d" , &i ,&j ); printf("%d ,%d\n" , i ,j); fclose(pf); } 而Input.txt有一些值。 有人能给我建议一种在运行程序后获取input.txt的

我必须通过编程从输入文本文件中读取一些值

int main(){

    FILE *pf;
    int i;
    int j;   

    pf = fopen("input.txt" , "r");

    fscanf(pf ,"%d , %d" , &i ,&j );

    printf("%d ,%d\n" , i ,j);

    fclose(pf);
}
而Input.txt有一些值。 有人能给我建议一种在运行程序后获取input.txt的方法吗

例如:

Open a terminal 
compile the code
Run the code
---Here it should ask for the file name---

命令行参数:

int main(int argc, char *argv[]) {
  std::cout << argv[1] << '\n';
}
intmain(intargc,char*argv[]){
std::cout示例:

    #include <fstream>
    #include <iostream>

    int main()
    {
       std::string filename;
       cin >> filename;
       ifstream inFile;
       inFile.open(filename.append(".txt");
       int a;
       while (inFile)
       {
          inFile >> a;
          cout << a;
       }
       return 0;
    }
#包括
#包括
int main()
{
std::字符串文件名;
cin>>文件名;
河流充填;
infle.open(filename.append(“.txt”);
INTA;
while(填充)
{
填充>>a;

CUTE你用C++来标记这个,但是你的代码看起来像纯C.,你使用C还是C++?@ dout。他显然只使用C库。如果它是纯C,它不会编译,因为他没有main函数的返回语句。我编辑了这样的程序:STD::String文件名;STD::CUT>文件名;STD::CUTE我编辑了如下代码std::string filename;std::cout>filename;std::cout有没有办法只从用户那里获取文件名并在pf=fopen(filename,“r”);?@Rdvsm是的,你可以说
fopen((filename+“.txt”).c_str(),“r”);
filename+=“.txt”;fopen(filename.c_str(),“r”);
是否有办法只从用户处获取文件名,并在pf=fopen(文件名,“r”)处追加.txt?
    #include <fstream>
    #include <iostream>

    int main()
    {
       std::string filename;
       cin >> filename;
       ifstream inFile;
       inFile.open(filename.append(".txt");
       int a;
       while (inFile)
       {
          inFile >> a;
          cout << a;
       }
       return 0;
    }