I have a text file that I want to tokenize(Separate the words) into a structure defined as

typedef struct 
{
	int fileno;
	char word[30];
	int position;
}storagefile[10000];

I want to be able to accesses each word, its position and the file it is in.

Can anyone tell me how to do this???

>>int position
should be size_t position because negative positions are not valid and size_t will allow for files twice the size (about) as int.

>> char word[]
use std::string here to avoid the many problems with character arrays.

use a vector instead of that huge array because vector will expand as needed.

call tellg() to get the position then the >> operator to read the word.

struct storagefile
{
	int fileno;
	std::string word;
	size_t position;
};
vector<storagefile> sf;
storagefile item;
ifstream in("file.txt");
memset(&item, 0, sizeof(item));
item.fileno = 1;
while( in >> item.word )
{
    sf.push_back(item);
    // get ready to read the next word
    item.position = in.tellg();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.