Click here to Skip to main content
16,005,236 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionfiles stream & iterators Pin
ganesh_IT25-Aug-10 3:54
ganesh_IT25-Aug-10 3:54 
AnswerRe: files stream & iterators Pin
Cedric Moonen25-Aug-10 4:12
Cedric Moonen25-Aug-10 4:12 
GeneralRe: files stream & iterators Pin
Rob Grainger25-Aug-10 5:35
Rob Grainger25-Aug-10 5:35 
GeneralRe: files stream & iterators Pin
Cedric Moonen25-Aug-10 7:08
Cedric Moonen25-Aug-10 7:08 
AnswerRe: files stream & iterators Pin
Maximilien25-Aug-10 4:24
Maximilien25-Aug-10 4:24 
AnswerRe: files stream & iterators Pin
prasad_som26-Aug-10 2:42
prasad_som26-Aug-10 2:42 
AnswerRe: files stream & iterators Pin
Sauro Viti25-Aug-10 4:34
professionalSauro Viti25-Aug-10 4:34 
AnswerRe: files stream & iterators PinPopular
Aescleal25-Aug-10 8:31
Aescleal25-Aug-10 8:31 
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
AnswerRe: files stream & iterators Pin
Niklas L25-Aug-10 20:34
Niklas L25-Aug-10 20:34 
QuestionGLFW Across multiple files Pin
Chris Copeland25-Aug-10 3:31
mveChris Copeland25-Aug-10 3:31 
AnswerRe: GLFW Across multiple files Pin
Chris Copeland25-Aug-10 3:34
mveChris Copeland25-Aug-10 3:34 
QuestionHelp with Standard Input Pin
Danzy8325-Aug-10 1:13
Danzy8325-Aug-10 1:13 
AnswerRe: Help with Standard Input PinPopular
CPallini25-Aug-10 1:36
mveCPallini25-Aug-10 1:36 
AnswerRe: Help with Standard Input Pin
carter200025-Aug-10 3:05
carter200025-Aug-10 3:05 
GeneralRe: Help with Standard Input Pin
CPallini25-Aug-10 3:23
mveCPallini25-Aug-10 3:23 
QuestionHide TilteBar CMFCToolBar window Pin
Benjamin Bruno25-Aug-10 0:42
Benjamin Bruno25-Aug-10 0:42 
AnswerRe: Hide TilteBar CMFCToolBar window Pin
Sauro Viti25-Aug-10 1:15
professionalSauro Viti25-Aug-10 1:15 
QuestionSize of SDI window in MDI applcation Pin
Anu_Bala24-Aug-10 23:13
Anu_Bala24-Aug-10 23:13 
QuestionCString Pin
raju_shiva24-Aug-10 19:23
raju_shiva24-Aug-10 19:23 
AnswerRe: CString PinPopular
Niklas L24-Aug-10 19:43
Niklas L24-Aug-10 19:43 
AnswerRe: CString [modified] Pin
Shivanand Gupta24-Aug-10 20:38
Shivanand Gupta24-Aug-10 20:38 
GeneralRe: CString Pin
goorley24-Aug-10 22:34
goorley24-Aug-10 22:34 
GeneralRe: CString PinPopular
CPallini24-Aug-10 23:28
mveCPallini24-Aug-10 23:28 
AnswerRe: CString PinPopular
CPallini24-Aug-10 21:18
mveCPallini24-Aug-10 21:18 
GeneralRe: CString Pin
raju_shiva24-Aug-10 21:49
raju_shiva24-Aug-10 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.