files stream & iterators
-
Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator
Please explain what you are trying to achieve. Files and iterators are two completely different concepts.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator
something like that (snatch from a google search):
std::ifstream ifs("filename.txt");
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());?
Watched code never compiles.
-
Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator
-
Please explain what you are trying to achieve. Files and iterators are two completely different concepts.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Really? I was under the impression that istream_iterator and ostream_iterator were for exactly that purpose.
-
Really? I was under the impression that istream_iterator and ostream_iterator were for exactly that purpose.
Looking at the other replies, I realized that I misunderstood his question. I thought that he wanted to serialize a container and use the iterators to save the data. that's why I asked him more information.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator
At the risk of sounding glib, you can use stream iterators with any stream, whether they're from memory, sockets, files. So if I have a lump of integers in a vector:
int init_data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> data( &int_data[0], &int_data[10] );I can pump them out to any stream using the copy algorithm. So:
std::copy( data.begin(), data.end(), std::ostream_iterator<int>( std::cout, "\n" ) );
will write that lot out to std::cout separated by newlines. If you replace std::cout with the name of an ofstream then those values will tbe written to the file instead. Replace it with the name of an ostringstream and the data will end up in a string. To read from a stream is much the same, except you use an istream_iterator:
std::vector<int> data;
std::copy( std::istream_iterator<int>( std::cin ), std::istream_iterator<int>(), std::back_inserter( data ) );will read integers from standard in until something goes wrong with the stream (end of file, I/O error, run out of memory) and dump them at the back of data. And of course it's not just copy - you can use any std algorithm that can handle input/output iterators. So as two examples you can transform a file into another one or accumulate the values in another. Basically any single pass operation you can do on a list or vector you can do on a file or pair of files in the same manner. Cheers, Ash
-
Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator
If you want to learn about iterators, I can recommend that you implement a few. It's a great way of learning what it is, the difference between the various types, forward/random access/bidirectional/etc, and in what situations an iterator is a good or bad choice. It's also a great lesson in efficiency as well as a way of getting to know stl in general a bit better.
-
something like that (snatch from a google search):
std::ifstream ifs("filename.txt");
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());?
Watched code never compiles.
Maximilien wrote:
std::ifstream ifs("filename.txt");std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator());
Something like this is much faster,
std::ifstream stream("c:\\test.txt");
if (stream.good())
{
std::stringbuf buf;
stream>>&buf;
std::string str(buf.str());
stream.close();
}