Adding words from a text file to a vector c++





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?



Am I doing this a bad way? Could this be made simpler?



void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;

string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}

for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}









share|improve this question


















  • 1





    You should use fileContents.push_back(word); instead of fileContents[p] = word; also change vector<string> fileContents(500); to vector<string> fileContents; and get rid of p

    – drescherjm
    Nov 27 '18 at 0:03













  • @drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place

    – jake
    Nov 27 '18 at 0:06








  • 1





    @jake Did you get rid of (500)?

    – 0x499602D2
    Nov 27 '18 at 0:06











  • It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2

    – jake
    Nov 27 '18 at 0:07




















1















I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?



Am I doing this a bad way? Could this be made simpler?



void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;

string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}

for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}









share|improve this question


















  • 1





    You should use fileContents.push_back(word); instead of fileContents[p] = word; also change vector<string> fileContents(500); to vector<string> fileContents; and get rid of p

    – drescherjm
    Nov 27 '18 at 0:03













  • @drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place

    – jake
    Nov 27 '18 at 0:06








  • 1





    @jake Did you get rid of (500)?

    – 0x499602D2
    Nov 27 '18 at 0:06











  • It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2

    – jake
    Nov 27 '18 at 0:07
















1












1








1








I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?



Am I doing this a bad way? Could this be made simpler?



void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;

string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}

for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}









share|improve this question














I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?



Am I doing this a bad way? Could this be made simpler?



void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;

string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}

for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}






c++ list c++11 vector






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 0:01









jakejake

235




235








  • 1





    You should use fileContents.push_back(word); instead of fileContents[p] = word; also change vector<string> fileContents(500); to vector<string> fileContents; and get rid of p

    – drescherjm
    Nov 27 '18 at 0:03













  • @drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place

    – jake
    Nov 27 '18 at 0:06








  • 1





    @jake Did you get rid of (500)?

    – 0x499602D2
    Nov 27 '18 at 0:06











  • It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2

    – jake
    Nov 27 '18 at 0:07
















  • 1





    You should use fileContents.push_back(word); instead of fileContents[p] = word; also change vector<string> fileContents(500); to vector<string> fileContents; and get rid of p

    – drescherjm
    Nov 27 '18 at 0:03













  • @drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place

    – jake
    Nov 27 '18 at 0:06








  • 1





    @jake Did you get rid of (500)?

    – 0x499602D2
    Nov 27 '18 at 0:06











  • It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2

    – jake
    Nov 27 '18 at 0:07










1




1





You should use fileContents.push_back(word); instead of fileContents[p] = word; also change vector<string> fileContents(500); to vector<string> fileContents; and get rid of p

– drescherjm
Nov 27 '18 at 0:03







You should use fileContents.push_back(word); instead of fileContents[p] = word; also change vector<string> fileContents(500); to vector<string> fileContents; and get rid of p

– drescherjm
Nov 27 '18 at 0:03















@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place

– jake
Nov 27 '18 at 0:06







@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place

– jake
Nov 27 '18 at 0:06






1




1





@jake Did you get rid of (500)?

– 0x499602D2
Nov 27 '18 at 0:06





@jake Did you get rid of (500)?

– 0x499602D2
Nov 27 '18 at 0:06













It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2

– jake
Nov 27 '18 at 0:07







It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2

– jake
Nov 27 '18 at 0:07














2 Answers
2






active

oldest

votes


















2














You could also use a more direct approach, copying immediately from the input stream.



std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);

std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));

return fileContents;
}





share|improve this answer































    1














    @drescherjm in the comments gave me the correct answer.



    void loadFile(string fileName)
    {
    vector<string> fileContents;
    ifstream file;
    file.open(fileName);
    if (!file.is_open()) return;

    string word;
    while (file >> word)
    {
    fileContents.push_back(word);
    }

    for (int i = 0; i < fileContents.size(); i++)
    {
    cout << fileContents[i] << endl;
    }
    }





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490887%2fadding-words-from-a-text-file-to-a-vector-c%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You could also use a more direct approach, copying immediately from the input stream.



      std::vector<std::string> loadFile(std::string fileName) {
      std::ifstream file(fileName);
      assert(file);

      std::vector<std::string> fileContents;
      std::copy(std::istream_iterator<std::string>(file),
      std::istream_iterator<std::string>(),
      std::back_inserter(fileContents));

      return fileContents;
      }





      share|improve this answer




























        2














        You could also use a more direct approach, copying immediately from the input stream.



        std::vector<std::string> loadFile(std::string fileName) {
        std::ifstream file(fileName);
        assert(file);

        std::vector<std::string> fileContents;
        std::copy(std::istream_iterator<std::string>(file),
        std::istream_iterator<std::string>(),
        std::back_inserter(fileContents));

        return fileContents;
        }





        share|improve this answer


























          2












          2








          2







          You could also use a more direct approach, copying immediately from the input stream.



          std::vector<std::string> loadFile(std::string fileName) {
          std::ifstream file(fileName);
          assert(file);

          std::vector<std::string> fileContents;
          std::copy(std::istream_iterator<std::string>(file),
          std::istream_iterator<std::string>(),
          std::back_inserter(fileContents));

          return fileContents;
          }





          share|improve this answer













          You could also use a more direct approach, copying immediately from the input stream.



          std::vector<std::string> loadFile(std::string fileName) {
          std::ifstream file(fileName);
          assert(file);

          std::vector<std::string> fileContents;
          std::copy(std::istream_iterator<std::string>(file),
          std::istream_iterator<std::string>(),
          std::back_inserter(fileContents));

          return fileContents;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 4:12









          KostaKosta

          1,771323




          1,771323

























              1














              @drescherjm in the comments gave me the correct answer.



              void loadFile(string fileName)
              {
              vector<string> fileContents;
              ifstream file;
              file.open(fileName);
              if (!file.is_open()) return;

              string word;
              while (file >> word)
              {
              fileContents.push_back(word);
              }

              for (int i = 0; i < fileContents.size(); i++)
              {
              cout << fileContents[i] << endl;
              }
              }





              share|improve this answer




























                1














                @drescherjm in the comments gave me the correct answer.



                void loadFile(string fileName)
                {
                vector<string> fileContents;
                ifstream file;
                file.open(fileName);
                if (!file.is_open()) return;

                string word;
                while (file >> word)
                {
                fileContents.push_back(word);
                }

                for (int i = 0; i < fileContents.size(); i++)
                {
                cout << fileContents[i] << endl;
                }
                }





                share|improve this answer


























                  1












                  1








                  1







                  @drescherjm in the comments gave me the correct answer.



                  void loadFile(string fileName)
                  {
                  vector<string> fileContents;
                  ifstream file;
                  file.open(fileName);
                  if (!file.is_open()) return;

                  string word;
                  while (file >> word)
                  {
                  fileContents.push_back(word);
                  }

                  for (int i = 0; i < fileContents.size(); i++)
                  {
                  cout << fileContents[i] << endl;
                  }
                  }





                  share|improve this answer













                  @drescherjm in the comments gave me the correct answer.



                  void loadFile(string fileName)
                  {
                  vector<string> fileContents;
                  ifstream file;
                  file.open(fileName);
                  if (!file.is_open()) return;

                  string word;
                  while (file >> word)
                  {
                  fileContents.push_back(word);
                  }

                  for (int i = 0; i < fileContents.size(); i++)
                  {
                  cout << fileContents[i] << endl;
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 27 '18 at 0:08









                  jakejake

                  235




                  235






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490887%2fadding-words-from-a-text-file-to-a-vector-c%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen