Inconsistent Application Behaviour depending on directory

Okay...

So I am running MSVC++ 2010 on Windows 7. I created an application that reads a text file from a directory, and stores it as an object which is used and edited by the application. I compiled the latest version in release mode, and it passed with flying colours when tested.

Then I copied it to a directory elsewhere on the computer, and ran it. Strangely enough, everything worked fine, except the code which detects the data state of the line (ie, is it a begin/end command or a comment), now thinking that everything is not important data (when the begin/end commands are clearly denoting it as being so)

The class in question, just for reference

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
#pragma once
#include "fileparse.h"

class Indexed_string
{	public:
	Indexed_string();
	Indexed_string(std::string i_string, unsigned int i_Index, bool i_is_data);
	std::string iString;	// The assigned value of where the string is in our arbitrary index (starting at 0 of course)
	unsigned int Index;		// Very useful because contents of the Text in memory can be protected from outside access, but retrieved by
	bool is_data;			// firing an index number to access at a soon to be created function...				
	~Indexed_string();			
};							

class TText :public CFileParse
{	public:
	TText(void);
	TText(std::string exe_path, std::string file_offset); 
	void Load_file(std::string file_offset);
	void Process_input(std::string line, unsigned int counter, bool Override_flag);
	std::string Access_data(unsigned int index);
	void Output_Data();
	void Insert_data(std::vector<std::string> i);
	void Insert_data(std::string i);
	std::string Get_path();
	unsigned int Get_data_index();
	unsigned int Get_file_index();
	bool Read_state();
	bool Check_file_exists();
	void Create_file();
	bool Save_file(std::string file_path, bool selfdestruct);
	~TText(void);
	private:
	std::string file_path;
	bool i_flag;
	std::vector<Indexed_string> text_index;		// The full text of the file, including comments, read cues, etc. 
	unsigned int data_index;					// The number of readable data elements in the file parse
	unsigned int file_index;					// The number of elements in the file parse, regardless of whether they should be read or not
	bool is_read;								// Was the file input succesful, and is the text still loaded?
};


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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "TText.h"
using namespace std;

Indexed_string::Indexed_string()
{
}

Indexed_string::Indexed_string(std::string i_string, unsigned int i_Index, bool i_is_data)
{	iString = i_string;	
	Index = i_Index;		
	is_data = i_is_data;	
}

Indexed_string::~Indexed_string()
{	iString.clear();
}


TText::TText(void)
{	

}

TText::TText(std::string exe_path, std::string file_offset)
{	data_index = 0;
	is_read = false;
	file_path = "NULL";
	Load_file(exe_path.append(file_offset));
}

void TText::Load_file(std::string file_offset)
{	ifstream file (file_offset);
	if (file.is_open())
	{	is_read = true;	// Indicate that the file is now copied to memory, and all has not gone to hell in a handbasket
		file_path = file_offset;
		std::string line;
		unsigned int counter = 0;
		while (std::getline(file,line))
		{	Process_input(line, counter, false);
			counter++;	// Nom, nom, nom, tasty bytes of data
		}	line.clear();
	}
	file.close();
}

void TText::Process_input(std::string line, unsigned int counter, bool Override_flag)
{	std::string comp_line = line;
	std::vector<Indexed_string>::iterator it;
	transform (comp_line.begin (), comp_line.end (), comp_line.begin (), toupper);	
	if(Override_flag == true)
	{	text_index.emplace_back(Indexed_string(line, counter, true));
	}
	else
	{	if (((comp_line == "BEGIN"))&&(i_flag == false))
		{	i_flag = true;	// Flip the read switch on, now the program will begin to accept text values each line & push their index back
			text_index.emplace_back(Indexed_string(line, counter, false));
		}
		else if (((comp_line == "END"))&&(i_flag == true))
		{	i_flag = false;	// Flip the read switch off at the end of the text block to close off the input flow
			text_index.emplace_back(Indexed_string(line, counter, false));	// Note that the Begin and End statements are ignored by the parser as well, assuming they are flipping i_flag
		}	// its like water, except the parts of it which are not like water, which is all of it
		else if ((comp_line.front() == '/')&&(comp_line.at(1) == '/'))
		{	text_index.emplace_back(Indexed_string(line, counter, false));
		}	// Commented!!! Sadly only effective on full line comments, but who cares!
		else if (i_flag == true)
		{	text_index.emplace_back(Indexed_string(line, counter, true));
			data_index++;
		}	// The good stuff :)
		else
		{	text_index.emplace_back(Indexed_string(line, counter, false));
		}	// Not commented or a parser flag, but not relevant data if not included in a read block
	}
	file_index++;
}	

void TText::Insert_data(std::vector<std::string> i)
{	Process_input("Begin", (file_index+1), false); 
	for(std::vector<std::string>::iterator it = i.begin(); it != i.end(); ++it)
	{	Process_input(*it,(file_index+1), true);
	}
	Process_input("End", (file_index+1), false);
}

void TText::Insert_data(std::string i)
{	Process_input("Begin", (file_index+1), false);
	Process_input(i,(file_index+1), true);
	Process_input("End", (file_index+1), false);
}

std::string TText::Access_data(unsigned int index)
{	unsigned int cy = 0;
	for(std::vector<Indexed_string>::iterator it = text_index.begin(); it != text_index.end(); ++it)
	{	if (it->is_data == true)
		{	if (cy == index)
			{	const std::string output = it->iString;
				return output;
			}
			else
			{	cy++;
			}
		}
	}
	return "BAD_INDEX";	
}

void TText::Output_Data()
{	for(std::vector<Indexed_string>::iterator it = text_index.begin(); it != text_index.end(); ++it)
	{	cout << it->Index << "	" << it->is_data << "	" << it->iString << endl;
	}
}

std::string TText::Get_path()
{	return file_path;
}

unsigned int TText::Get_data_index()
{	return data_index;
}	// Pretty much what it says on the box.

unsigned int TText::Get_file_index()
{	return file_index;
}	// Pretty much what it says on the llama

bool TText::Read_state()
{	return is_read;
}	// A check on whether the read was successful

bool TText::Check_file_exists()
{	bool Check_return = false;
	ifstream file (file_path);
	if(file.is_open())
	{	Check_return = true;
	}	file.close();
	return Check_return;
}

bool TText::Save_file(std::string file_path, bool selfdestruct)
{	switch(Check_file_exists())
	{	case true:
		{	std::remove(file_path.c_str());
			ofstream file (file_path);
			if (file.is_open())
			{	for (std::vector<Indexed_string>::iterator it = text_index.begin(); it != text_index.end(); ++it)
				{	file << ((it->iString).append("\n"));	
				}
			}
			file.close();
			// It read funny without a comment in here.
			if (selfdestruct == true)
			{	this->~TText();
			}
			else
			{	return true;
			}
		}
		case false:
		{	return false;
		}
	}
}

TText::~TText(void)
{	text_index.clear();
	data_index = 0;
	i_flag = false;		// I honestly have no idea why
	is_read = false;
}


What the heck is going on? Why would the location of a executable file affect its operation in memory?
¿and the client code?

> Why would the location of a executable file affect its operation in memory?
You may be changing the current working directory, and relative references now refer to other places.
Last edited on
Client code?

But the file is loading fine in memory, I could see that when I used output data() on it. The only issue appears to be when Process_input() is called, and the data is always listed as is_data = false;

The working directory shouldn't affect that in any way, right?
> Client code?
how you are using your class.
http://www.eelis.net/iso-c++/testcase.xhtml
Topic archived. No new replies allowed.