473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

copy and istream_iterator question

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 constructors in streambuf.

I'd like to use algorithms instead of a loop, but I don't know if that
is possible. Any ideas?
Thanks in advance.

--------------------------------------------------

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

using namespace std;

istream& operator>> (istream& is, stringbuf& sb)
{
is.get(sb);
return is;
}

int main()
{
vector<stringbuf> v;
ifstream f;
f.open(__FILE__);
cout << __FILE__ << endl;
istream_iterator<stringbuf> istart(f),iend;
v.insert(v.begin(),istart,iend);
cout << v.size() << endl;

return 0;
}

Dec 16 '05 #1
6 8460
George wrote:
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 constructors in streambuf.

I'd like to use algorithms instead of a loop, but I don't know if that
is possible. Any ideas?
Thanks in advance.

--------------------------------------------------

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

using namespace std;

istream& operator>> (istream& is, stringbuf& sb)
{
is.get(sb);
return is;
}

int main()
{
vector<stringbuf> v;
ifstream f;
f.open(__FILE__);
cout << __FILE__ << endl;
istream_iterator<stringbuf> istart(f),iend;
v.insert(v.begin(),istart,iend);
cout << v.size() << endl;

return 0;
}


Since you describe yourself as a C++/STL new-comer, I'll recommend that
you drop manipulating stringbufs and just go with strings. Then you can
do something like this:

#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;

int main()
{
ifstream f( "text.txt" );
if( !f ) return -1;

vector<string> v;
copy( istream_iterator<string>( f ),
istream_iterator<string>(),
back_inserter( v ) );

// Now v contains all the strings from f
// ...
return 0;
}

I'd suggest you pick up Koenig and Moo's _Accelerated C++_, the best
book for learning STL-based C++ programming from the ground up, and
Josuttis' _The C++ Standard Library - A Tutorial and Reference_.

Cheers! --M

Dec 16 '05 #2
On 2005-12-16, George <ge**********@excite.com> wrote:
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
constructors in streambuf.

I'd like to use algorithms instead of a loop, but I don't know
if that is possible. Any ideas? Thanks in advance.


Here's a program closely modeled on yours, using std::string
instead of std::stringbuf, and std::copy.

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

int main()
{
std::vector<std::string> v;
std::ifstream f(__FILE__);
std::cout << __FILE__ << '\n';
std::istream_iterator<std::string> start(f), finish;
std::copy(start, finish, std::back_inserter(v));
std::cout << v.size() << '\n';
return 0;
}

You don't need to use stringbuf to use istream_iterator. Perhaps
you needed it for something else.

You can pass the filename to the constructor of the file streams.
If the file is not found, an exception will be thrown.

Check out back_inserter, and some of the other adaptors.

You normally don't want to insert into a vector, unless it's at
the end iterator. A back_inserter calls push_back, which is the
most efficient way to build up a vector.

There are other ways to count the strings in a file that won't
need to build up a vector. Here's one:

#include <fstream>
#include <string>
#include <algorithm>
#include <functional>

template <typename T>
struct yes: std::unary_function<T, bool>
{
bool operator()(const T&) const { return true; }
};

int main()
{
std::ifstream f(__FILE__);
std::cout << __FILE__ << '\n';
std::istream_iterator<std::string> start(f), finish;
std::cout << std::count_if(start, finish, yes<std::string>()) << '\n';
return 0;
}

You could also write your own stateful functor and use
std::for_each. I believe the standard doesn't actually bless this
practice... yet. The problem is that implementations aren't
prohibited form copying your functor. In practice, you're very
unlikely to face problems.

--
Neil Cerutti
Dec 16 '05 #3
Wow Neil, this is great stuff.

But istream_iterator<string> just takes word by word, delimiting on
spaces. Is there a way to read the whole line into a string?

What is the difference between copy(,,back_inserter<v>) and
v.insert(v.begin(),,)?

Well, I'll not push my graces. Thanks much for listening.

Sincerely, George

Dec 16 '05 #4

George wrote in message ...
Wow Neil, this is great stuff.

But istream_iterator<string> just takes word by word, delimiting on
spaces. Is there a way to read the whole line into a string?
Delimit on '\n', or use getline():

// includes here
int main(){
std::vector<std::string> vStr;
std::ifstream in(__FILE__);
if( not in ){ /* throw std::exception(); */ return EXIT_FAILURE;} //if(!in)
std::string line;
while( getline( in, line ) ){ // load the file
vStr.push_back(line);
} // while()
// do something with vStr here.
return 0;
} // main() end

What is the difference between copy(,,back_inserter<v>) and
v.insert(v.begin(),,)?
About 10 seconds per mil on an 33Mhz machine. <G>

Well, I'll not push my graces. Thanks much for listening.
Sincerely, George


--
Bob R
POVrookie
--
Dev-C++ IDE: http://www.bloodshed.net/
MinGW (GNU compiler): http://www.mingw.org/
MinGWStudio http://www.parinyasoft.com/
wxWidgets URL: http://www.wxwidgets.org
V IDE & V GUI: http://www.objectcentral.com/
Quincy IDE 2005 URL: http://pipou.net/down/Quincy2005Project.zip
POVray: http://www.povray.org/
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Dec 16 '05 #5
George wrote:
Wow Neil, this is great stuff.

But istream_iterator<string> just takes word by word, delimiting on
spaces. Is there a way to read the whole line into a string?


Yes -- a couple of them. A fairly simple one is to define a proxy class
for the purpose:

class line {
std::string buffer;
public:
friend operator>>(std::istream &is, line &li) {
std::getline(is, li);
return is;
}
operator std::string() const { return buffer; }
};

Extracting an object of type line reads an entire line. In most cases,
you'll immediately convert the result to a std::string, something like:

std::vector<std::string> raw_data;

std::copy(
std::istream_iterator<line>(stream),
std::istream_iterator<line>(),
std::back_inserter(raw_data));

A completely different method is to define a ctype facet that only
classifies new-line and possibly carriage return (but NOT space, tab,
etc.) as whitespace. You then use imbue to make the stream use a locale
including this facet. The normal operator>> for std::string reads
whitespace delimited sequences, so if only new-lines are whitespace,
that means it reads entire lines instead of words. While this second
method works perfectly well, I generally prefer the proxy. The primary
time for considering the ctype is if you consistently need to treat
data a line at a time.

--
Later,
Jerry.

Dec 17 '05 #6
Awesome all. Thanks so much for the solutions.

Dec 19 '05 #7

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

Similar topics

9
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because...
14
by: dover | last post by:
/*Copy the line a token at a time into the output*/ copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(oss, " ")); What's the meaning of this statement?...
6
by: ma740988 | last post by:
There's no way to use the STL algorithm copy to print an outfile (essentially an ofstream)? So now: int main() { std::ifstream InFile( "exercise15.txt"); std::ofstream ToFile( "NewFile.txt"...
7
by: SIgnOff | last post by:
What the fastest way to copy strings from file to a vector in STL?
0
by: Richo11 | last post by:
This is probably a silly question but I am learning sorry... I am trying to read in a series of strings from standard input using the istream_iterator member function, how does...
3
by: varun.chinta | last post by:
Hi, I am doing a project which involves basic file operations like file open, file copy, file rename, file remove. can anyone suggest me how to do a file copy. The file that i create using the...
4
by: pedagani | last post by:
I want to copy only a part of the binary file delimited by offset values say START_OFFSET_ & END_OFFSET_ which can be as huge as a 50 Giga. Below is the program where I could set a START_OFFSET_...
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...
9
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.