簡體   English   中英

使用 Ifstream c++ 打開 csv 文件

[英]Open csv file using Ifstream c++

我正在嘗試使用 ifstream 打開 csv 文件,但找不到正確的方法 I git 這個錯誤:沒有給出有效的輸入文件,請檢查給定的文件名

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, vector<string>& infolabels, char separator = ';') {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(Error::StsBadArg, error_message);
    }
...
}

我稱之為 function:

const string fn_csv = "C:\\Users\\XXX\\AndroidStudioProjects\\Application\\app\\src\\main\\res\\raw\\face.txt";
    read_csv(fn_csv, images, labels,infolabels);

您可能想檢查文件是否存在於您提供的路徑中。 這是您可以嘗試的東西

  std::string line;
  std::ifstream myfile ("C://somefolder//yourfile.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      std::cout << line << '\n';
    }
    myfile.close();
  }

  else 
     std::cout << "Unable to open file";

此外,無需使用 c_str() 轉換為 C 字符串。 ifstream 構造函數接受 std::string 對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM