Please help a poor fellow out, tokenizer

I am having hard time getting next line from my tokenizer

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  
//-----------------------------------------------------------------------------
// Tokenizer default constructor
// Read a file and store entire file
Tokenizer::Tokenizer(istream& inputStream) : instream(inputStream){
	//read entire data into memory
	string tempf;
	tempf = string((std::istreambuf_iterator<char>( instream )),
            (std::istreambuf_iterator<char>()) );
	istringstream stream(tempf);
	lines = new string[300];
	//read a line
	string temp;
	getline(stream, temp);
	lines[0] = temp;
	for(int i = 1; i < 301; i++){
		getline(stream, temp);
		string firstValue;
		istringstream instr(temp);
		getline(instr, firstValue , ',');
		if(firstValue == "-1"){
			lines [i] = temp;
			break;
		}
	}
	point = 0;
	istringstream streamWord(lines[point]);
	transaction = readToken(streamWord);
}
//-----------------------------------------------------------------------------
// Tokenizer readToken
// reads a token t from input stream inputstream and
// returns whether the read was successful
bool Tokenizer::readToken(istream& instr){
	string temp;
	getline(instr, temp);
	setValue(temp);
	if (transaction){
		return true;
	}
	return false;
}


//-----------------------------------------------------------------------------
// Tokenizer setValue
// set the value of the transaction to each data
// parameter is a line of file that include all the transaction data
void Tokenizer::setValue(string temp) {

	//line = temp;
	string word;
	istringstream streamWord(temp);
	getline(streamWord, word, ',');
	if (word == "-1"){
		eof = true;
		transaction = false;
		getline(streamWord, word, ',');
		istringstream(word) >> taxYear;
	}
	else {
		eof = false;
		transaction = true;
		token = new string[6];
		token[0] = word;
		for (int i = 1; i < 7; i++){
			getline(streamWord, word, ',');
			token[i] = word;
		}
		istringstream(token[0]) >> year;
		istringstream(token[1]) >> month;
		istringstream(token[2]) >> day;
		action = token[3];
		security =token[4];
		istringstream(token[5]) >> share;
		istringstream(token[6]) >> price;
	}
}

//-----------------------------------------------------------------------------
// Tokenizer nextLine
// tokenize next line of the file
void Tokenizer::nextLine(){
	if (!eof){
		point += 1;
		string temp;
		istringstream streamWord(lines[point]);
		transaction = readToken(streamWord);
	}
}
Last edited on
closed account (48T7M4Gy)
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
[code]#include <stdio.h>
#include <string.h>
#include <iostream>

const int MAX_LINE_LENGTH = 100;

int main(int argc, char* argv[])
{
	FILE* source;
	FILE* destination;

	char line[ MAX_LINE_LENGTH ];
	char separators[]   = "?!. ,\t\n";
	char* token;

	char* format = "+%s+ ";

	source = fopen( "source.txt", "r" ); // a random text file for demo purposes
	destination = fopen( "destination.txt", "w" );

	if(  source != NULL )
	{
		while( fgets( line, MAX_LINE_LENGTH, source ) != NULL )
		{
			token = strtok( line, separators );
			while( token != NULL )
			{
				printf( format, token ); // for demo purposes
				fprintf( destination, format, token ); // for demo purposes
				token = strtok( NULL, separators );
			}
		}
		printf("\n +++ ENDS +++\n"); // for demo purposes
	}
	else
		printf( "fgets error\n" );

	fclose( source );
	fclose( destination );

	return 0;
}
[/code]

This is a very simple tokenizer program that works.
Topic archived. No new replies allowed.