c++ - 在exe上拖动文件后,ifstream无法打开文件

标签 c++ parameter-passing ifstream

我有以下问题:

ifstream 无法打开文件时,我将文件拖放到我的工具 (exe) 中。
如果我通过控制台手动提供它,它就可以工作!
我不知道差异在哪里,因为我正在切断路径并只传递文件名。
看一下代码:

int main(int argc, char* argv[]) {
if (argc < 2) {
    cout
        << "ERROR: Wrong amount of arguments! Give at least one argument ...\n"
        << endl;
    cout << "\n" << "Programm finished...\n\n" << endl;
    cin.ignore();
    exit(1);
    return 0;
    }

    vector<string> files;

   for (int g = 1; g < argc; g++) {
      string s = argv[g];
      cout<<"parameter at: " << g << " = " << argv[g] << "\n" << endl;

      string filename = "";
      int pos = s.find_last_of("\\", s.size());

      if (pos != -1) {
          filename.append(s.substr(pos + 1));
//         cout<<" cutted path: " << s.substr(0,s.size()-filename.size()) << endl;

//            cout << "argv[1] " << argv[1] << endl;
          cout << "\n filename: " << filename << "\t pos: " << pos << endl;
          files.push_back(filename);

          }
      files.push_back(s);
      }


  for (unsigned int k = 0; k < files.size(); k++)
      {
      cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl;
      Converter a(files.at(k).c_str());
      a.getCommandsFromCSV();
      a.saveConvertedFile();
      }

  cout << "\n" << "Programm finished...\n\n" << endl;

  cin.ignore();

  return 0;
}  

它已经在构造函数上失败了:

Converter::Converter(const char* file) {
  filename = file;
  myfile.open(filename.c_str(), ios_base::in);

  cout << (myfile ? "open successful on constructor " : "some error on constructor");
  cin.ignore();

  trace_raw = "";
}  

你知道为什么吗?


更新:
作为参数的文件现在可以使用了。解决方案是保留完整路径。
无论如何,我在硬编码文件上有同样的错误。我认为它可能是相同的,这就是为什么我在文件名的开头添加 .\ ......但没有成功。
代码:

void GenericCommandConverter::getATCommandsFromCSV() {

  cout << "\t| +++++++++++getATCommandsFromCSV() started+++++++++++++ |"
    << endl;

 /*
  * CSV file name is hardcoded
  */
  string filename_csv = ".\\test.csv";
  string commands = "";
  int pos_start = 0;
  int pos_end = 0; // "|"
  int substrLength = 0;
  int separator_count = 0;

  char c;

  vector<string> lines;
  vector<string> commandList;
  vector<vector<string> > linesSeparated;


  ifstream csvFile;
  csvFile.open(filename_csv.c_str(), ios_base::in);

  cout << (myfile ? "open successful on getATCommandsFromCSV " : "some error on   getATCommandsFromCSV ");
  cin.ignore();  
      ...  

更新 2: 解决方案是:将文件拖放到 exe 时,“根”文件夹会更改为拖放文件所在的文件夹。为硬编码文件提供 *.exe 的路径解决了这个问题!

最佳答案

我猜你当前的目录是错误的。不要切断路径。无论如何,您应该进行错误检查/调试以查看它无法打开文件的原因。勤奋调试对于解决问题至关重要,而不必盲目猜测。

关于c++ - 在exe上拖动文件后,ifstream无法打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4885504/

相关文章:

c++ - 使用临时对象来初始化线程

c++ - 在 glVertexAttribPointer 的模板中获取成员偏移量

C++读取一个字符串后跟两个 double

c++ - std::ifstream 读取大数的错误大小

c++ - 通过指针读取多维数组或 vector 是否安全?

c++ - 如何使 vector 的元素独一无二? (删除不相邻的重复项)

powershell - 是否可以在一行 PowerShell 中启动具有多个选项卡的 Microsoft Edge?

c++ - 通过引用将数组从 Delphi 7 传递到 C++ Dll

java - Java 是否允许将 getter 作为方法参数传递?

C++ Ifstream 读太多?