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

c++ how to read a file and split it's lines

我正在尝试读取一个文件并拆分每一行(所以我将 "I"、"walk"、"my"、[而不是行 "I walk my dog every day" =24=]、"every"、"day")。我尝试按照 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

这个有效:

 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>{}};

问题很大,说不通。您尝试初始化 token,但这已经完成(上面的第 15 行)。要么选择 copy 方法,要么需要重新初始化向量:

tokens = std::vector<std::string>{istream_iterator<string>(iss), 
                                  istream_iterator<string>()}; 

请注意,您使用 token-vektor 的第二种方式为文件中的每一行重新初始化。根据后面的使用,看看copy().

您正在使用编译器尚无法理解的语法。

  1. 使用更新的编译器

  2. 使用旧语法:

        tokens = vector<string>( istream_iterator<string>(istringstream( line )),
                 istream_iterator<string>() );