在读取txt文件里的文件名时,用作后续文件的路径拼接字符时一定要注意,要把每个txt文件中一些影藏字符给去掉,比如第一个字符为' ',或者是有换行符,一定要去掉,否则就会读取文件失败,还找不到问题在哪。给个txt读取没问题的例子
void Crowdsourcing::Readfiletxt(std::string path, std::vector<std::string>& filemessage)
{char buffer[7000];std::ifstream in(path.c_str());std::vector<std::string> tempsave;tempsave.resize(7000);int index = 0;if (!in.is_open()){//std::cout << "no swc file" << std::endl;in.close();return ;}int nullline = 0;while (!in.eof()){in.getline(buffer, 7000);if (buffer[0] == '\0' || buffer == ""){nullline++;if (nullline > 10)break;continue;}else{nullline = 0;}std::string line = std::string(buffer);if (!line.empty()){int tmpindex = 0;int firstindex = -1;for (int li = 0; li < line.length(); li++){if (line[li] == '\r' || line[li] == ' '){}else{if (firstindex == -1)firstindex = li;tmpindex = li;}}if (firstindex != -1)line = line.substr(firstindex, tmpindex + 1);elsecontinue;/*line.erase(0, line.find_first_not_of(" "));line.erase(line.find_last_not_of(" ") + 1);line.erase(line.find_last_not_of('\r') + 1);*/}tempsave[index] = line;index++;if (index >= 7000){std::copy(tempsave.begin(), tempsave.end(), std::back_inserter(filemessage));tempsave.clear();tempsave.resize(7000);index = 0;}//filemessage.push_back(line);}std::copy(tempsave.begin(), tempsave.begin() + index, std::back_inserter(filemessage));in.close();return ;
}