Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. read from file

read from file

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nish Nishant

    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

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #6

    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

    N 1 Reply Last reply
    0
    • C Christian Graus

      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

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #7

      ****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

      C 2 Replies Last reply
      0
      • N Nish Nishant

        ****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

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #8

        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

        1 Reply Last reply
        0
        • N Nish Nishant

          ****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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #9

          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

          N 1 Reply Last reply
          0
          • C Christian Graus

            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

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #10

            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

            C 1 Reply Last reply
            0
            • N Nish Nishant

              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

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #11

              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

              N 1 Reply Last reply
              0
              • C Christian Graus

                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

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #12

                ****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

                C 1 Reply Last reply
                0
                • N Nish Nishant

                  ****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

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #13

                  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

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    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

                    J Offline
                    J Offline
                    Jamie Hale
                    wrote on last edited by
                    #14

                    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

                    C 1 Reply Last reply
                    0
                    • J Jamie Hale

                      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

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #15

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups