read from file
-
CG My thinking is that when someone asks a question on how to read a text file, he must be looking for the easiest solution, otherwise he would not ask a question about reading a text file, which would be something covered in every C++ book ever written. And the MFC classes are the easiest way to read a file unless you want to use VB or Delphi. Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
Nish [BusterBoy] wrote: otherwise he would not ask a question about reading a text file, which would be something covered in every C++ book ever written. This does not follow - how many 'in every book and easy to find in MSDN' questions do we answer here ? Personally I find the IOStreams method I posted as easy and elegant as it gets, but I'm a one eyed fan of anything that's standard. :rose: 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
Nish [BusterBoy] wrote: otherwise he would not ask a question about reading a text file, which would be something covered in every C++ book ever written. This does not follow - how many 'in every book and easy to find in MSDN' questions do we answer here ? Personally I find the IOStreams method I posted as easy and elegant as it gets, but I'm a one eyed fan of anything that's standard. :rose: 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
****Christian Graus wrote: Personally I find the IOStreams method I posted as easy and elegant as it gets, but I'm a one eyed fan of anything that's standard. To be honest, all that STL scared me. I couldnt make head or tail of the code you posted to the poor fella who only wanted to open a text file and read in a few numbers. Am the only one here who does not understand or use STL? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
****Christian Graus wrote: Personally I find the IOStreams method I posted as easy and elegant as it gets, but I'm a one eyed fan of anything that's standard. To be honest, all that STL scared me. I couldnt make head or tail of the code you posted to the poor fella who only wanted to open a text file and read in a few numbers. Am the only one here who does not understand or use STL? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
Nish [BusterBoy] wrote: To be honest, all that STL scared me I admit I felt the same when I started using it. But then, I felt that way about MFC as well.... Nish [BusterBoy] wrote: Am the only one here who does not understand or use STL? Sadly, I'd say you're in the majority. STL is SO much nicer than MFC container classes, but people are put off by the unfamiliar syntax and seem just to avoid learning it. 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
****Christian Graus wrote: Personally I find the IOStreams method I posted as easy and elegant as it gets, but I'm a one eyed fan of anything that's standard. To be honest, all that STL scared me. I couldnt make head or tail of the code you posted to the poor fella who only wanted to open a text file and read in a few numbers. Am the only one here who does not understand or use STL? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
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 file_contents; This defines a vector ( an array ) of strings. copy(istream_iterator(str),istream_iterator(), 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(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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
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 file_contents; This defines a vector ( an array ) of strings. copy(istream_iterator(str),istream_iterator(), 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(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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
Now that you have explained it, it sounds good. I mean you can do so much with so little code. Hmmmm. Maybe if I ever survive my COM learning attempts I might try out some simple STL too :-) Thanks CG Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
Now that you have explained it, it sounds good. I mean you can do so much with so little code. Hmmmm. Maybe if I ever survive my COM learning attempts I might try out some simple STL too :-) Thanks CG Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
Nish [BusterBoy] wrote: I might try out some simple STL too Join us... don't be afraid.... Seriously, it's not hard once you get into it, I spent a lot more time trying to fathom out COM than I did STL. And obviously I'll be here to help if you get stuck.. 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
Nish [BusterBoy] wrote: I might try out some simple STL too Join us... don't be afraid.... Seriously, it's not hard once you get into it, I spent a lot more time trying to fathom out COM than I did STL. And obviously I'll be here to help if you get stuck.. 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
****Christian Graus wrote: Join us... don't be afraid.... LOL That sounds like right out of a thriller movie. You are a revolutionary leader. I am the cool hero confused as to whether I should continue being a handsome playboy screwing french women or whether I should join the revolution. Finally I give in and we take over the solar system. You are made king and I become a count [you give me one of the jovian moons] and marry a beautiful princess born and brought up in Mars and live happily ever after Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
****Christian Graus wrote: Join us... don't be afraid.... LOL That sounds like right out of a thriller movie. You are a revolutionary leader. I am the cool hero confused as to whether I should continue being a handsome playboy screwing french women or whether I should join the revolution. Finally I give in and we take over the solar system. You are made king and I become a count [you give me one of the jovian moons] and marry a beautiful princess born and brought up in Mars and live happily ever after Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
Nish [BusterBoy] wrote: Christian Graus wrote: Join us... don't be afraid.... LOL That sounds like right out of a thriller movie. Actually I'm quoting Dilbert, the pointy haired boss to be exact. But yours sounds like more fun.... 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
First of all, please only ask the one question once. Secondly, the following code should work, assuming the numbers are properly delimited. It currently reads in strings, but you can use atoi, or convert it to read int's, I am sure. This program is my playground for using IOStreams, so I sure there are #include and using std:: statements in there that are not needed.
#include "stdafx.h"
#include #include #include #include #include #include using std::copy;
using std::vector;
using std::string;
using std::ifstream;
using std::ostream_iterator;
using std::cout;
using std::back_inserter;
using std::getline;using std::ostringstream;
using std::istream_iterator;int main(int argc, char* argv[])
{
string currentLine;ifstream str("c:\\\\winzip.log"); if (!str.is\_open()) { cout << "It's stuffed\\n"; return -1; } vector file\_contents; copy(istream\_iterator(str),istream\_iterator(), back\_inserter(file\_contents));
// Now print out the results
copy(file_contents.begin(), file_contents.end(), ostream_iterator(cout, "\n"));return 0;
}
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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
Nice. By the simplicity of the original post (he didn't ask how to find 4000-digit primes in constant time), I would have guessed they were asking for the simple fopen(), fgets(), etc. But this is a nice solution. Prolly waaaaay over the poster's head. J
-
Nice. By the simplicity of the original post (he didn't ask how to find 4000-digit primes in constant time), I would have guessed they were asking for the simple fopen(), fgets(), etc. But this is a nice solution. Prolly waaaaay over the poster's head. J
Jamie Hale wrote: I would have guessed they were asking for the simple fopen(), fgets(), etc. I had a look at those functions last night ( fulfilling a contract at the last minute, he wanted me to use them ), and I thought they looked way ugly. I try to avoid C functions whenever I can. Jamie Hale wrote: But this is a nice solution. Prolly waaaaay over the poster's head. Probably. My hope was by showing an elegant C++ way of doing it that he may well dig deeper and analyse why it all works. That's how I learned most of the standard C++ stuff I know, by asking questions on comp.lang.c++, making a fool of myself often, and spending a lot of time trying to figure out what the answers I was given meant. 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.
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now