file stream problems!

Okay so I'm completely stumped. I'm writing a program that parses a .txt file and counts the # of words and how many times they appear. My first problem happens during my prompt to the user to input the directory and file name. It works great... for 1 file in 1 specific directory. here's the code:

1
2
3
4
5
6
7
8
9
10
	do 
	{
		cout << "Enter an input file name: ";
		getline(cin, directory);
		inputFile.open(directory.c_str());
		if(!inputFile.is_open())
		{
			cout << "Could not open requested file, please try again.\n";
		}
	}while(!inputFile.is_open());


It will only load one file, "e:\cs161\notes\100201.txt", ANYTHING else I've tried returns an error.

The other problem occurs later, when I'm parsing the file. If I open the only working file on the FIRST ATTEMPT, it runs fine.. but if I have "failed attempts" and it asks the user to try again, it freezes up and goes into an infinite loop. Here's the code it happens at:

1
2
3
4
5
6
7
8
9
10
	while(!inputFile.eof())
	{
		getline(inputFile, fileLine);
		currentStream << fileLine;
		lines++;
		while(!currentStream.eof())
		{
			wordsTotal++;
			getline(currentStream, currentWord, ' ');
			if(wordParse.size() == 0)


It never seems to be able to get into the 2nd while statement. If I open the file on the first try on running the program it goes through perfect to the end and spits out to me how many lines and words are in the file.

I'll post the entire code at the end. There's still a lot to do but I want to get it passed this point first. I don't want to build the rest of the functions until it is to a point where it is opening any file I request, and is properly parsing it. But as of now I'm completely stumped as to why this is happening. Especially the first one. So if anyone can help me clear this up I'd be hugely appreciative... thanks :)

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
typedef struct
{
	string word;
	int count;
	size_t size;
} wordData;

// FUNCTION PROTOTYPES
string directoryRequest();
void outputHeader(string, int, int, int);
void fileParse(vector<wordData> &, ifstream &, int &, int &, int &);
void outputData(vector<wordData> &, int, int, int);
int main()
{
	int wordsTotal, wordsDistinct, lines;
	wordsTotal = 0;
	wordsDistinct = 0;
	lines = 0;
	string directory;
	vector<wordData> wordParse;
	wordData vectorDefault = {"", 0, 0};
	ifstream inputFile;

	do 
	{
		cout << "Enter an input file name: ";
		getline(cin, directory);
		inputFile.open(directory.c_str());
		if(!inputFile.is_open())
		{
			cout << "Could not open requested file, please try again.\n";
		}
	}while(!inputFile.is_open());


	fileParse(wordParse, inputFile, wordsTotal, wordsDistinct, lines);
	inputFile.close();
	outputHeader(directory, wordsTotal, wordsDistinct, lines);
	
	return 0;
}

void fileParse(vector<wordData>& wordParse, ifstream& inputFile, 
			   int& wordsTotal, int& wordsDistinct, int& lines)
{
	string fileLine, currentWord;
	stringstream currentStream;
	unsigned int idx;
	int vectorSize;
	char test;
	wordData vectorDefault = {"", 0, 0};
	while(!inputFile.eof())
	{
		getline(inputFile, fileLine);
		currentStream << fileLine;
		lines++;
		while(!currentStream.eof())
		{
			wordsTotal++;
			getline(currentStream, currentWord, ' ');
			if(wordParse.size() == 0)
			{
				wordsDistinct++;
				wordParse.push_back(vectorDefault);
				wordParse[wordParse.size() - 1].word = currentWord;
				wordParse[wordParse.size() - 1].size = currentWord.size();
			}
			vectorSize = wordParse.size();
			for(idx = 0;idx < vectorSize;idx++)
			{
				if(currentWord == wordParse[idx].word)
				{
					wordParse[idx].count++;
					idx = vectorSize;
				}
				else if(idx == (wordParse.size() - 1))
				{
					wordsDistinct++;
					wordParse.push_back(vectorDefault);
					wordParse[wordParse.size() - 1].count++;
					wordParse[wordParse.size() - 1].word = currentWord;
					wordParse[wordParse.size() - 1].size = currentWord.size();
													
				}
			}
		}
		currentStream << "";
		currentStream.clear();
	}
	return;
}
//string directoryRequest()
//{
//	string directory;
//	cout << "Enter an input file name: ";
//	getline(cin, directory);
//	return directory;
}
void outputHeader(string directory, int wordsTotal, int wordsDistinct, int lines)
{
	cout << "C S  1 6 1  F I L E  I N D E X  G E N E R A T O R" << endl
		 << "=================================================" << endl
		 << "File Name: \"" << directory << "\"" << endl
		 << "Total Lines: " << lines << endl
		 << "Total Words: " << wordsTotal << endl
		 << "Distinct Words: " << wordsDistinct << endl
		 << "=================================================" << endl;
	return;
}
No one has any ideas? Anyone willing to test it on another compiler? I'm using microsoft visual C++ 8.0 express.

Btw I fixed the second problem. However the first problem where it returns only errors when it tries to open any file but certain ones in a specific directory are still an issue.
Last edited on
Hm, can't spot any mistake here. (some unnecessary things like bookeeping the size of words, but I guess that's for the other functions you add later you were talking about ;) ) . The code runs fine for me.

Just a random guess: I didn't know you can use a stringstream simultanously for input and output before. Maybe it's the source of your problem? (But actually, I rather doubt it)
thanks for the reply imi. At this point I'm chumming it up to system/compiler specific issues. And you are correct, the bookkeeping of word sizes is for later functions which I've stalled in writing because I've been pulling my hair out over this issue. Going to move to finishing up the assignment from a pc that doesn't produce the same errors.

Have had a couple people run the code flawlessly on their personal systems with different compilers, so something about my specific system seems to be making it difficult. >_< Bed time though. Today was supposed to be writing a paper day, not trying to fix a seemingly non-existent bug day :D
Some other guy got similar problems. Maybe this is related to http://www.cplusplus.com/forum/general/20464/
Topic archived. No new replies allowed.