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

2.1k views Asked by At

I'm trying to read a file and split each line (so instead of the line "I walk my dog every day" I would have "I", "walk", "my", "dog", "every", "day"). I tried to follow the example from here.

This is my code:

    #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 << ", " ;
    }
}

Right now my code does not compile, probably because of the istringstream, and I get this error:

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

There are 2 answers

3
camelCase On BEST ANSWER

This works:

 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  }

This approach works also for multilined files, and puts all words into the same vector.

The line

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

is very problematic and doesn't make sense. You try to initialize token, but that has been done already (line 15 above). Either you choose the copy-approach, or you need to reinitialise the vector:

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

Please note, that you reinitialize for each line in your file with the second way your token-vektor. Depending on the later use, have a look at the copy().

0
Robert Andrzejuk On

You are using syntax which the compiler does not undestand yet.

  1. Use a newer compiler

  2. Use older syntax:

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