read .txt file line by line and convert to array

i am working on a text twist like program on visual studio 2008 c++ or devc++ and i'm trying to figure out how would i access the list of words from a text file line by line or maybe convert it into an array so that i can access it by array[number] form

text file looks like this

ant
bag
can
dip
eat
fog
etc


dictionary word list composed of 3, 4, 5, 6 and 7-letter words
so its a big file if i manually put it into an array

i just want to access it per line for word comparison

____


i already made an anagram and code with very few words stored in an array and try working the program and it worked!

now my wish is how to access the word list text file?
i cant make it like this

string Word_list[]={"bag",
"can",
"dip",
"eat",
",fog",
"etc"};


the list of words contains tens of thousands of words!=)


any help would be greatly appreciated...
I would use a standard container for that ( documentation at http://www.cplusplus.com/reference/stl/ )

the resulting code will appear like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <map> 
// Here I'm using a map but you could choose even other containers
#include <fstream>
#include <string>
using namespace std;
map<int,string>WordList//int is the key, string the returnad value
int GetWordList(char* file)
{
    char getch;
    int wordindex=-1;
    string tempstring="";
    ifstream myFile(file);
    while (!myFile.eos())
    {
         myFile.get(getch);
         if (getch=='\r') continue; // skipping '\r' characters
         if (getch == '\n' || myFile.eos() )
         {
               WordList[++wordindex]=tempstring;
               tempstring="";
         }else  tempstring+=getch;
    }
    return wordindex; //returns the maximum index
}
// now words can be accessed like this WordList[n]; where 'n' is the index 

In <algorithm> you should find some sorting operations ( http://www.cplusplus.com/reference/algorithm/ )
Last edited on
This is where a std::deque really shines.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <deque>
using namespace std;

struct word_list
  {
  deque <string> words;

  word_list( const string& filename ) { load( filename ); }

  void load( const string& filename ) {
    words.clear();
    if (!filename.empty())
      {
      ifstream f( filename.c_str() );
      copy(
        istream_iterator <string> ( f ),
        istream_iterator <string> (),
        back_inserter( words )
        );
      }
    }

  bool valid( const string& word )
    {
    return find( words.begin(), words.end(), word ) != words.end();
    }
  };

int main()
  {
  word_list words( "words.txt" );

  if (words.valid( "hello" ) && words.valid( "world" ))
    cout << "Hello world!\n";
  else
    cout << "fooey!\n";

  return 0;
  }

Of course, the strings are compared normally (with case-sensitivity), so you may want to transform input to lowercase (or whatever your words file uses) first:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
#include <ccytpe>
#include <functional>
...

string lowercase( const string& s )
  {
  string result( s );
  transform(
    result.begin(),
    result.end(),
    result.begin(),
    ptr_fun <int, int> ( tolower )
    );
  return result;
  }

Hope this helps.
thank you all for the reply

ok i get the point about containers like map and vector
my question is where should i usually put my text file here?
i tried to put it where i think it should be but i cant get it working...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <map> 
// Here I'm using a map but you could choose even other containers
#include <fstream>
#include <string>
using namespace std;
map<int,string>WordList//int is the key, string the returnad value
int GetWordList(char* file)
{
    char getch;
    int wordindex=-1;
    string tempstring="";
    ifstream myFile(file);
    while (!myFile.eos())
    {
         myFile.get(getch);
         if (getch=='\r') continue; // skipping '\r' characters
         if (getch == '\n' || myFile.eos() )
         {
               WordList[++wordindex]=tempstring;
               tempstring="";
         }else  tempstring+=getch;
    }
    return wordindex; //returns the maximum index
}
// now words can be accessed like this WordList[n]; where 'n' is the index  

You could probably also just use getline() in this case to get everything from the file and store it into the container of your choice.
Topic archived. No new replies allowed.