Click here to Skip to main content
15,926,035 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalread from file Pin
marouane miftah el kheir27-Jan-02 16:32
marouane miftah el kheir27-Jan-02 16:32 
GeneralRe: read from file Pin
Nish Nishant27-Jan-02 16:51
sitebuilderNish Nishant27-Jan-02 16:51 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 17:10
protectorChristian Graus27-Jan-02 17:10 
GeneralRe: read from file Pin
Nish Nishant27-Jan-02 17:28
sitebuilderNish Nishant27-Jan-02 17:28 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 17:44
protectorChristian Graus27-Jan-02 17:44 
GeneralRe: read from file Pin
Nish Nishant27-Jan-02 17:49
sitebuilderNish Nishant27-Jan-02 17:49 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 17:58
protectorChristian Graus27-Jan-02 17:58 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 18:05
protectorChristian Graus27-Jan-02 18:05 
Let me walk you through how easy it is:

string currentLine;

The above line is irrelevant, it's left over from a line by line method I played with.


ifstream str("c:\\winzip.log");
if (!str.is_open())
{
cout << "It's stuffed\n";
return -1;
}
The above just opens a file and checks if it's OK.

vector<string> file_contents;

This defines a vector ( an array ) of strings.

copy(istream_iterator<string>(str),istream_iterator<string>(), back_inserter(file_contents));

This is the magic - the first argument means to iterate through the file a string at a time ( i.e. it does not expect only numbers, but any valid characters ), the second tells it to keep looking until the end of the file ( the return of the default constructor is the eof, back_inserter tells it to add the result to the end of the container specified, and the container to be copied into is our array.

// Now print out the results
copy(file_contents.begin(), file_contents.end(), ostream_iterator<string>(cout, "\n"));

This simply copies the entire array to the ouput stream. Pretty sweet, don't you think ? How else could you print an entire array, or read an entire file into an array, with one line of code ?

It's the algorithms that plug into the STL containers that make it light years ahead of MFC containers.


Christian

I have come to clean zee pooollll. - Michael Martin Dec 30, 2001

Picture the daffodil. And while you do that, I'll be over here going through your stuff.

Picture a world without war, without hate. And I can picture us attacking that world, because they would never expect it.
<Center><A href = "http://www.sonork.com" target = "_Blank">Sonork</A> ID 100.10002:MeanManOz</Center><Center>I live in <U><B>Bob's HungOut</B></U> now</center>
GeneralRe: read from file Pin
Nish Nishant27-Jan-02 18:21
sitebuilderNish Nishant27-Jan-02 18:21 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 19:26
protectorChristian Graus27-Jan-02 19:26 
GeneralRe: read from file Pin
Nish Nishant27-Jan-02 19:37
sitebuilderNish Nishant27-Jan-02 19:37 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 20:12
protectorChristian Graus27-Jan-02 20:12 
GeneralRe: read from file Pin
Christian Graus27-Jan-02 16:54
protectorChristian Graus27-Jan-02 16:54 
GeneralRe: read from file Pin
Jamie Hale28-Jan-02 5:05
Jamie Hale28-Jan-02 5:05 
GeneralRe: read from file Pin
Christian Graus28-Jan-02 8:44
protectorChristian Graus28-Jan-02 8:44 
GeneralInstalling a callback between two applications Pin
Mr.Freeze27-Jan-02 15:52
Mr.Freeze27-Jan-02 15:52 
GeneralRe: Installing a callback between two applications Pin
27-Jan-02 16:09
suss27-Jan-02 16:09 
GeneralRe: Installing a callback between two applications Pin
Mr.Freeze27-Jan-02 17:47
Mr.Freeze27-Jan-02 17:47 
GeneralRe: Installing a callback between two applications Pin
27-Jan-02 18:39
suss27-Jan-02 18:39 
Generalread from file Pin
marouane miftah el kheir27-Jan-02 15:27
marouane miftah el kheir27-Jan-02 15:27 
GeneralRandom Numbers :: C++ Pin
valikac27-Jan-02 15:00
valikac27-Jan-02 15:00 
GeneralRe: Random Numbers :: C++ Pin
Michael Dunn27-Jan-02 17:22
sitebuilderMichael Dunn27-Jan-02 17:22 
GeneralRe: Random Numbers :: C++ Pin
markkuk27-Jan-02 19:44
markkuk27-Jan-02 19:44 
GeneralRe: Random Numbers :: C++ Pin
valikac28-Jan-02 4:37
valikac28-Jan-02 4:37 
GeneralHelp with Win32, but its in the VB forum Pin
David Wengier27-Jan-02 14:53
David Wengier27-Jan-02 14:53 

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.