-5

How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?

Andrew Vu
  • 1
  • 2
  • possible duplicate of [Split a string in C++?](http://stackoverflow.com/questions/236129/split-a-string-in-c) –  Feb 23 '15 at 00:03

1 Answers1

1

Simply:

#include <fstream>
#include <iostream>

int main()
{
  std::fstream file("table1.txt");

  std::string word;
  while (file >> word)
    {
      // do whatever you want, e.g. print:
      std::cout << word << std::endl;
    }

  file.close();

  return 0;
}

word variable will contain every single word from a text file (words should be separated by space in your file).

Marcin Kolny
  • 633
  • 7
  • 14