c ++如何读取文件并拆分它的行

时间:2017-09-07 08:53:21

标签: c++ istream-iterator

我试图读取一个文件并拆分每一行(所以而不是行#34;我每天都会遛狗#34;我会#34;我",&# 34;步行","我的","狗","每个","日")。我试图关注here中的示例。

这是我的代码:

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <sstream>
    #include <vector>

using namespace std;

int main()
{
    ifstream  geo;
    string line;
    geo.open("file.txt", ios::in );
    istringstream* iss;
    vector<string> tokens;

    while(!geo.eof())
    {
        getline(geo ,line);
        iss = new istringstream(line); 
        tokens = vector<string>(istream_iterator<string>(*iss),
                                      istream_iterator<string>{});

        for (auto i:tokens)
                cout << i << ", " ;
    }
}

现在我的代码无法编译,可能是因为istringstream,我收到此错误:

g++ -fPIC -Wall -std=c++1y `root-config --cflags` -I./include -O2  `root-config --cflags --glibs`  obj/ClipperDraw.o obj/clipper.o bin/cliipperex.cpp -o exe/cliipperex 
bin/cliipperex.cpp: In function ‘int main()’:
bin/cliipperex.cpp:187:57: error: no matching function for call to ‘std::istream_iterator<std::basic_string<char> >::istream_iterator(<brace-enclosed initializer list>)’
   tokens = {istream_iterator<string>{istringstream(line)},
                                                         ^
bin/cliipperex.cpp:187:57: note: candidates are:
In file included from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/iterator:66:0,
                 from bin/cliipperex.cpp:13:
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stream_iterator.h:72:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&) [with _Tp = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int]
       istream_iterator(const istream_iterator& __obj)
       ^
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stream_iterator.h:72:7: note:   no known conversion for argument 1 from ‘std::istringstream {aka std::basic_istringstream<char>}’ to ‘const std::istream_iterator<std::basic_string<char> >&’
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stream_iterator.h:68:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int; std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]
       istream_iterator(istream_type& __s)
       ^
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stream_iterator.h:68:7: note:   no known conversion for argument 1 from ‘std::istringstream {aka std::basic_istringstream<char>}’ to ‘std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>&}’
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stream_iterator.h:64:26: note: constexpr std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator() [with _Tp = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int]
       _GLIBCXX_CONSTEXPR istream_iterator()
                          ^
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stream_iterator.h:64:26: note:   candidate expects 0 arguments, 1 provided
bin/cliipperex.cpp:187:10: error: no match for ‘operator=’ (operand types are ‘std::vector<std::basic_string<char> >’ and ‘<brace-enclosed initializer list>’)
   tokens = {istream_iterator<string>{istringstream(line)},
          ^
bin/cliipperex.cpp:187:10: note: candidates are:
In file included from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/vector:69:0,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/random.h:34,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/random:50,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stl_algo.h:65,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/algorithm:62,
                 from bin/cliipperex.cpp:12:
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/vector.tcc:160:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >]
     vector<_Tp, _Alloc>::
     ^
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/vector.tcc:160:5: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const std::vector<std::basic_string<char> >&’
In file included from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/vector:64:0,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/random.h:34,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/random:50,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stl_algo.h:65,
                 from /home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/algorithm:62,
                 from bin/cliipperex.cpp:12:
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stl_vector.h:439:7: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >]
       operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
       ^
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stl_vector.h:439:7: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::basic_string<char> >&&’
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stl_vector.h:461:7: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >]
       operator=(initializer_list<value_type> __l)
       ^
/home/user/anaconda3/envs/deepjetLinux3/include/c++/4.8.3/bits/stl_vector.h:461:7: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::initializer_list<std::basic_string<char> >’
Makefile:13: recipe for target 'exe/cliipperex' failed
make: *** [exe/cliipperex] Error 1

2 个答案:

答案 0 :(得分:2)

这有效:

ID  ID_PARENT   NAME    LINK    CHILDREN_NUMBER
1   NULL        Main    /       2
2   1           SUB1    /Sub1   3
3   1           SUB2    /Sub2   0
4   2           SUB1.1  /Sub1.1 0
5   2           SUB1.2  /Sub1.2 0
6   2           SUB1.3  /Sub1.3 0

此方法也适用于多行文件,并将所有单词放在同一个 1 #include <iostream> 2 #include <fstream> 3 #include <algorithm> 4 #include <iterator> 5 #include <sstream> 6 #include <vector> 7 8 using namespace std; 9 10 int main() 11 { 12 ifstream geo; 13 string line; 14 geo.open("file.txt", ios::in ); 15 vector<string> tokens; 16 17 while(!geo.eof()) 18 { 19 getline(geo ,line); 20 istringstream iss(line); 21 copy(istream_iterator<string>(iss), 22 istream_iterator<string>(), 23 back_inserter(tokens) 24 ); 25 26 } 27 for (auto i:tokens) { 28 cout << i << ", " ; 29 } 30 cout << endl; 31 32 return 0; 33 }

该行

vector

是非常有问题的,没有意义。您尝试初始化tokens = {istream_iterator<string>{istringstream(line)}, istream_iterator<string>{}}; ,但已经完成(上面的第15行)。要么选择token - 方法,要么需要重新初始化向量:

copy

请注意,您使用第二种方式重新初始化文件中的每一行tokens = std::vector<std::string>{istream_iterator<string>(iss), istream_iterator<string>()}; - vektor。根据以后的使用情况,请查看token

答案 1 :(得分:1)

您正在使用编译器尚未解决的语法。

  1. 使用较新的编译器

  2. 使用旧语法:

    <form>
    
        <input type="text" id="inputTell" class="bfh-phone" data-format="(ddd) ddd-dddd">
    
    </form>