I’m tring to search a text file and extract the data after a heading. However, I’ve got some issues with iterators that I don’t know how to overcome.
This is a sample text file:
Relay States 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Ideally, I’d like to call LoadData<bool> something.LoadData("Relay States"); and have it return an std::vector with {0,0,0,0,0,0,0,0,…}.
template<typename T> std::vector<T> CProfile::LoadData(const std::string& name)
{
std::ifstream ifs(FILE_NAME);
std::vector<T> data;
std::istreambuf_iterator<char> iit = std::istreambuf_iterator<char>(ifs);
std::search(iit, ifs.eof(), name.begin(), name.end());
std::advance(iit, name.size() + 1);
T buffer = 0;
for(ifs.seekg(iit); ifs.peek() != '\n' && !ifs.eof(); data.push_back(ifs))
{
ifs >> buffer;
data.push_back(buffer);
}
return data;
}
From what I understand, the main problems with my code are:
- std::search is an ambiguous call, how would I go about fixing this?
- ifs.seekg(iit) is not legal, how would I go about making iit a valid argument?
Thanks.
Well I think your args to std::search is problem
should be
as for line :
ifs.seekg(iit)inforloop isn’t good , since seekg expects some offset of typestreamposnot an iterator. so it should beifs.seekg(0)