NX二次开发-读文件
1 bool ReadFile(const std::string& fileName, std::vector<std::string>& txtLines) 2 { 3 if (!CheckFileExist(fileName)) 4 { 5 return false; 6 } 7 8 std::ifstream ifs(fileName.c_str(), std::ios_base::in); 9 if (!ifs.is_open()) 10 { 11 return false; 12 } 13 14 std::string currentLine; 15 while (std::getline(ifs, currentLine)) 16 { 17 txtLines.push_back(currentLine); 18 } 19 ifs.close(); 20 return true; 21 }
1 bool CheckFileExist(const std::string& fileName) 2 { 3 if (fileName.empty()) 4 { 5 return false; 6 } 7 8 std::ifstream ifs(fileName.c_str(), std::ios_base::in); 9 10 if (ifs.is_open()) 11 { 12 ifs.close(); 13 return true; 14 } 15 else 16 { 17 return false; 18 } 19 }