Globbing in C++/C, on Windows

前端 未结 5 1828
礼貌的吻别
礼貌的吻别 2020-12-01 14:41

Is there a smooth way to glob in C or C++ in Windows?

E.g., myprogram.exe *.txt sends my program an ARGV list that has...ARGV[1]=*.txt in it.

5条回答
  •  不思量自难忘°
    2020-12-01 14:51

    This is very Windows-specific. I don't know how you'd write this to be cross-platform. But I've used this in Windows programs and it works well for me.

    // Change to the specified working directory
    string path;
    cout << "Enter the path to report: ";
    cin >> path;
    _chdir(path.c_str());
    
    // Get the file description
    string desc;
    cout << "Enter the file description: ";
    cin >> desc;
    
    // List the files in the directory
    intptr_t file;
    _finddata_t filedata;
    file = _findfirst(desc.c_str(),&filedata);
    if (file != -1)
    {
      do
      {
        cout << filedata.name << endl;
        // Or put the file name in a vector here
      } while (_findnext(file,&filedata) == 0);
    }
    else
    {
      cout << "No described files found" << endl;
    }
    _findclose(file);
    

提交回复
热议问题