У меня totalSortedRuns
количество файлов. Я хочу открыть их в векторе. Я использовал два метода для этого:
vector<ifstream> files;
const char * fileName;
for(int i = 1; i<=totalSortedRuns; i++){
fileName = (inputFileName + to_string(i)).c_str();
files[i].open(fileName);
}
Пробовал этот метод, потому что метод ниже не работал. Это вызывает ошибку сегментации при запуске, очевидно, потому что мой вектор files
пуст и я присваиваю значение его элементу 0-th
. Чтобы решить эту проблему, я попытался назначить files.push_back(NULL)
, но он дал мне ошибку
error: no matching function for call to ‘std::vector<std::basic_ifstream<char> >::push_back(NULL)’
Метод 2:
vector<ifstream> files;
ifstream file;
const char * fileName;
for(int i = 1; i<=totalSortedRuns; i++){
fileName = (inputFileName + to_string(i)).c_str();
file.open(fileName);
files.push_back(file); // line no 178 in code
}
Но это дает мне ошибку:
![enter image description here](https://i.stack.imgur.com/qY5Zs.png)
In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/c++allocator.h:33,
from /usr/include/c++/8/bits/allocator.h:46,
from /usr/include/c++/8/string:41,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/ios:42,
from /usr/include/c++/8/ostream:38,
from /usr/include/c++/8/iostream:39,
from invertedIndex.cpp:1:
/usr/include/c++/8/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::basic_ifstream<char>; _Args = {const std::basic_ifstream<char, std::char_traits<char> >&}; _Tp = std::basic_ifstream<char>]’:
/usr/include/c++/8/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::basic_ifstream<char>; _Args = {const std::basic_ifstream<char, std::char_traits<char> >&}; _Tp = std::basic_ifstream<char>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::basic_ifstream<char> >]’
/usr/include/c++/8/bits/stl_vector.h:1079:30: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_ifstream<char>; _Alloc = std::allocator<std::basic_ifstream<char> >; std::vector<_Tp, _Alloc>::value_type = std::basic_ifstream<char>]’
invertedIndex.cpp:178:29: required from here
/usr/include/c++/8/ext/new_allocator.h:136:4: error: use of deleted function ‘std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const std::basic_ifstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/stdc++.h:70,
from invertedIndex.cpp:6:
/usr/include/c++/8/fstream:552:7: note: declared here
basic_ifstream(const basic_ifstream&) = delete;
^~~~~~~~~~~~~~
Из строки invertedIndex.cpp:178:29: required from here
я понял, что ошибка в files.push_back(file)
. Я не смог понять, что именно это за ошибка и почему.
Может кто-нибудь сказать, пожалуйста, как это сделать (читать файлы в векторе)? Спасибо ...