473,472 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::istream_iterator to read whole lines?

I am writing a program to read in a file, do some processing, then write
it out to a different file. I really like the idiom I use for output:

// std::vector<std::string> vec;
// std::ofstream out;
std::copy(vec.begin(),
vec.end(),
std::ostream_iterator<std::string>(out, "\n"));

Is there a way to do the same thing for input that will read whole
lines? I tried the obvious thing:

// std::ifstream in;
std::copy(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>(),
std::back_inserter(vec));

but it separates on whitespace so every word is a different element in
the vector. I would like it to do the same thing as

// std::string line;
while (std::getline(in, line)) {
vec.push_back(line);
}
It's not a big deal or anything. I would just like the the two
operations to look consistent.

--
Marcus Kwok
Dec 1 '05 #1
2 7037
On 2005-12-01, Marcus Kwok <ri******@gehennom.net> wrote:
I am writing a program to read in a file, do some processing,
then write it out to a different file. I really like the idiom
I use for output:

// std::vector<std::string> vec;
// std::ofstream out;
std::copy(vec.begin(),
vec.end(),
std::ostream_iterator<std::string>(out, "\n"));

Is there a way to do the same thing for input that will read
whole lines? I tried the obvious thing:

// std::ifstream in;
std::copy(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>(),
std::back_inserter(vec));

but it separates on whitespace so every word is a different
element in the vector. I would like it to do the same thing as

// std::string line;
while (std::getline(in, line)) {
vec.push_back(line);
}


Implement a line-based input iterator. It should be a snap.

You could implement a line type instead, with a suitable
operator>>, but I don't like the idea as much.

--
Neil Cerutti
Dec 1 '05 #2
Neil Cerutti <le*******@email.com> wrote:
On 2005-12-01, Marcus Kwok <ri******@gehennom.net> wrote:
I am writing a program to read in a file, do some processing,
then write it out to a different file. I really like the idiom
I use for output:

// std::vector<std::string> vec;
// std::ofstream out;
std::copy(vec.begin(),
vec.end(),
std::ostream_iterator<std::string>(out, "\n"));

Is there a way to do the same thing for input that will read
whole lines? I tried the obvious thing:

// std::ifstream in;
std::copy(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>(),
std::back_inserter(vec));

but it separates on whitespace so every word is a different
element in the vector. I would like it to do the same thing as

// std::string line;
while (std::getline(in, line)) {
vec.push_back(line);
}


Implement a line-based input iterator. It should be a snap.


OK, my template technique is not too strong yet, so here is what I came
up with. Basically, I copied the implementation of istream_iterator
from the STL that comes with VS .NET 2003 (I believe they still use
Dinkumware) but renamed some of the implementation items, and added a
specialization for std::string, so this may not be the simplest or most
elegant way. If anyone knows a simpler way to specialize
istream_iterator for std::string then please enlighten me. Also, please
disregard some of the funky spacing, as I'm trying to get it to fit on
80 character lines for the NG post.

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>

template <class T,
class Ch = char,
class Tr = std::char_traits<Ch>,
class Dist = std::ptrdiff_t>
class my_istream_iterator : public std::iterator<std::input_iterator_tag,
T,
Dist,
const T*,
const T&> {
public:
typedef my_istream_iterator<T, Ch, Tr, Dist> my_t;
typedef Ch char_type;
typedef Tr traits_type;
typedef std::basic_istream<Ch, Tr> istream_type;

// construct singular iterator
my_istream_iterator() : my_istream(0) { }

// construct with input stream
my_istream_iterator(istream_type& s) : my_istream(&s) { getval(); }

// return designated value
const T& operator*() const { return my_val; }

// return pointer to class object
const T* operator->() const { return &**this; }

// preincrement
my_istream_iterator& operator++()
{
getval();
return *this;
}

// postincrement
my_istream_iterator operator++(int)
{
my_t tmp = *this;
++*this;
return tmp;
}

// test for iterator equality
bool equal(const my_t& rhs) const { return my_istream == rhs.my_istream; }

protected:
// get a T value if possible
void getval()
{
if (my_istream != 0 && !(*my_istream >> my_val))
my_istream = 0;
}

istream_type* my_istream; // pointer to input stream
T my_val; // lookahead value (valid if my_istream is not null)
};

// specialization for std::string
template <>
void my_istream_iterator<std::basic_string<char>, char,
std::char_traits<char>, std::ptrdiff_t>::getval()
{
if (my_istream != 0 && !(std::getline(*my_istream, my_val)))
my_istream = 0;
}

// my_istream_iterator template operators
// test for my_istream_iterator equality
template <class T, class Ch, class Tr, class Dist>
inline bool operator==(const my_istream_iterator<T, Ch, Tr, Dist>& lhs,
const my_istream_iterator<T, Ch, Tr, Dist>& rhs)
{
return lhs.equal(rhs);
}

template <class T, class Ch, class Tr, class Dist>
inline bool operator!=(const my_istream_iterator<T, Ch, Tr, Dist>& lhs,
const my_istream_iterator<T, Ch, Tr, Dist>& rhs)
{
return !(lhs == rhs);
}

int main(int argc, char* argv[])
{
std::ifstream in(argv[1]);
std::vector<std::string> vec;

std::copy(my_istream_iterator<std::string>(in),
my_istream_iterator<std::string>(),
std::back_inserter(vec));

std::ofstream out(argv[2]);
std::copy(vec.begin(),
vec.end(),
std::ostream_iterator<std::string>(out, "\n"));

return 0;
}

// vim: tabstop=4 shiftwidth=4

--
Marcus Kwok
Dec 2 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NPC | last post by:
Hi, Is there any way to use an istream_iterator<> in a way which inserts each element at the end of a newline character rather than a space character? Could be it looks for any type of whitespace...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
2
by: alberto | last post by:
I am learning STL with the book STL Tutorial and Reference guide (1 edition), the following example don't run : int main() { // Initialize array a with 10 integers: int a = {12, 3, 25, 7, 11,...
6
by: George | last post by:
Hi All, I'm trying to learn c++/stl. I'd like a fancy way to read lines of an ascii file into vector of stringbufs. I made a first attempt, but the compiler complains about private...
3
by: jmoy.matecon | last post by:
I get an error while compiling the following program: int main() { vector<int> v(istream_iterator<int>(cin), istream_iterator<int>()); copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));...
8
by: dragoncoder | last post by:
Hi, I am just a newbie in STL and trying to learn istream_iterator. My problem is I want a program which will take a comma separated string from the command line, tokenize it and copies into a...
5
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The...
2
by: Juha Nieminen | last post by:
I'm using gcc 3.3.5. This code: std::set<std::stringt(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); gives a strange error message: error: cannot use...
9
by: ahmadcorp | last post by:
Hi, This should be a simple problem, but I can't seem to figure it out. Maybe someone can help? I have a file containing data each line has about 6 entries (numbers) space delimited and each...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.