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. ATL / WTL / STL
  4. Read the contents of a file

Read the contents of a file

Scheduled Pinned Locked Moved ATL / WTL / STL
questiongraphicstutoriallearning
5 Posts 3 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.
  • A Offline
    A Offline
    Anil_vvs
    wrote on last edited by
    #1

    Hi All, Can anyone tell me how to read the contents (words) of a text file into a vector/ list? I may be asking a very basic question but I am just a beginner. Hope you all understand. Thanks and Regards, Anil

    S 1 Reply Last reply
    0
    • A Anil_vvs

      Hi All, Can anyone tell me how to read the contents (words) of a text file into a vector/ list? I may be asking a very basic question but I am just a beginner. Hope you all understand. Thanks and Regards, Anil

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      Try something like this: #include <iostream> #include <fstream> #include <string> #include <vector> #include <iterator> #include <algorithm>   int main() { using namespace std;   // Open the file. fstream fs("C:\\Words.txt"); if (!fs) { cerr << "Failed to open file!" << endl; return 1; }   // Our vector of words. vector<string> WordList;   // Place all the words in the vector. typedef istream_iterator<string> StringsIT; copy(StringsIT(fs), StringsIT(), back_inserter(WordList));   // Just for testing purposes dump all the word in the vector to the console. copy(WordList.begin(), WordList.end(), ostream_iterator<string>(cout, "\n"));   return 0; }

      Steve

      A L 2 Replies Last reply
      0
      • S Stephen Hewitt

        Try something like this: #include <iostream> #include <fstream> #include <string> #include <vector> #include <iterator> #include <algorithm>   int main() { using namespace std;   // Open the file. fstream fs("C:\\Words.txt"); if (!fs) { cerr << "Failed to open file!" << endl; return 1; }   // Our vector of words. vector<string> WordList;   // Place all the words in the vector. typedef istream_iterator<string> StringsIT; copy(StringsIT(fs), StringsIT(), back_inserter(WordList));   // Just for testing purposes dump all the word in the vector to the console. copy(WordList.begin(), WordList.end(), ostream_iterator<string>(cout, "\n"));   return 0; }

        Steve

        A Offline
        A Offline
        Anil_vvs
        wrote on last edited by
        #3

        Thanx Steve!!![:)]

        1 Reply Last reply
        0
        • S Stephen Hewitt

          Try something like this: #include <iostream> #include <fstream> #include <string> #include <vector> #include <iterator> #include <algorithm>   int main() { using namespace std;   // Open the file. fstream fs("C:\\Words.txt"); if (!fs) { cerr << "Failed to open file!" << endl; return 1; }   // Our vector of words. vector<string> WordList;   // Place all the words in the vector. typedef istream_iterator<string> StringsIT; copy(StringsIT(fs), StringsIT(), back_inserter(WordList));   // Just for testing purposes dump all the word in the vector to the console. copy(WordList.begin(), WordList.end(), ostream_iterator<string>(cout, "\n"));   return 0; }

          Steve

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          why would you do someone's class assignment for them? :wtf: led mike

          S 1 Reply Last reply
          0
          • L led mike

            why would you do someone's class assignment for them? :wtf: led mike

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            If it's really an assignment the teacher will know straight away: some who can't program in C++ properly using back inserters, stream iterators and algorithms is a dead giveaway. If your assumption is incorrect then I've helped someone out.

            Steve

            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