-4

Say that I have a .txt file with 5000 words one above the other. and I want to convert that list contained in the txt file into this form:

{"word1, "word2", "word3" ....."word5000"}

So that way I can use it as an array for C++.

Is there a way to do that? Any method is welcome , as long as it is an automated process. Thanks for reading!

MortyAndFam
  • 159
  • 1
  • 8
  • Welcome to SO. Please read [ask]. This question is too broad in scope. If you are beginning C++, maybe read some tutorials on file i/o and data structures and standard library collections. – OldProgrammer Aug 03 '14 at 16:03

1 Answers1

0

Use a vector instead of an array. Using it, the task looks something like this:

std::ifstream in("words.txt");

std::vector<std::string> words{ std::istream_iterator<std::string>(in),
                                std::istream_iterator<std::string>() };

Now the word that was on the first line of the file is in words[0], the second in words[1], and so on.

Note: if a line contains more than one word, this will read them as separate words. If you want the entire contents of a line treated as a single word, see the answers to a previous question specifically about how to do that.

Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111