C++文件选择窗口

C++ File Selection Window

本文关键字:窗口 选择 文件 C++      更新时间:2023-10-16

案例:我有几个文本文件,我需要有一个界面来选择它们到我的程序中。这是我写的代码

using namespace std;
vector<string> strings;
ifstream file("TestingRead.txt");
std::string str;
string myArray[5];
while (std::getline(file, str, ','))
{
    cout << str << "n";
    for (int i = 0; i <= 4; i++) {
        myArray[i] = str;
    }
    strings.push_back(str);
}

请看一下:ifstream文件("TestingRead.txt"(;无论如何要替换硬代码方法吗?可能是文件选择器窗口?我的操作系统在 Linux 上运行

你应该看看一个GUI库,比如Gtk+、Qt、wxWidget等。例如,GtkFileChooserDialog 是你想要的 Gtk+ 中。

如果只需要打开文件一次,则编写代码和仅使用命令行参数可能会更容易:

#include <fstream>
#include <iostream>
int main (int argc, char* argv[]) {
  if (argc < 2) {
    std::cerr << "Provide filepath: ./my_program file_name" << std::endl;
    return 1;
  }
  const std::string file_to_open (argv[1]);
  std::ifstream file (file_to_open.c_str());
  //  do some awesome stuff here
  return 0;
}