0

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???

2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

>>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();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.