File Manager Class Logic Problems

Certain member functions are not responding as they should.

Page 1 of 1

5 Replies - 2610 Views - Last Post: 27 July 2009 - 09:05 PM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

File Manager Class Logic Problems

Posted 23 July 2009 - 06:11 AM

Hello everyone, its been a while since I have been on the DIC forums.

I am currently designing a File Manager class to be used in an upcoming project. I have been testing the class for a few days to ensure that all my functions respond as expected. Naturally, they aren't.

Member functions designed to take the content of a file and deposit the data into a Vector of strings isn't working. I am not sure why. Im new to vectors, and GDB apparently cannot print the contents of a vector. So Debugging is useless :(

My test Main() is suppose to construct the class, then make calls to functions that would read the contents of the file into a storage vector<string>. Then, a loop would be used to print the contents of this storage buffer.

On execution this isn't working. I am not sure why, but debugging with GDb hasn't told me anything. I have looked over references for the vector container class and i seem to be following the rules.

My code is bellow. Thanks for the help everyone.
Its good to be back.

FileManager.h:
// Trial Source Code for FileManager.h

// ** NOT FOR RELEASE **

#ifndef FILEMANAGER_H
#define FILEMANAGER_H

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>

using namespace std;

class FileManager
{
public:
	/// ** Member Stream Objects **
	fstream MyFile;

	/// ** Constructor **
	FileManager(string arg1);

	/// ** Member Functions : Public **
	bool chkFile(void); // Is The File Availible?
	void GetFileCont(void); // Read Contents of File
	void PurgeFile(void); // Empty File
	void WriteFile(void); // Write FileCont to file
	void PutInFiCon(string  arg1); // Put into FileCont
	string ReadFrFiCont(int arg1); // Read a line From FileCont
	void GetFileName(void); // Get Name of file from filePath
	void SetFileName(string arg1); // Sets FileName

	// Return Private Data's
	string SayFileName(void); // Returns FileName
	string SayFilePath(void); // Returns FilePath

	// Misc Functions
	int SayLengthOfFileCont(void);

private:
	vector<string> FileContent;
	string  FileName;
	string  FilePath;
};
/// ===============================
FileManager::FileManager(string arg1)
{
	FilePath = arg1;
}

/// ===============================
int FileManager::SayLengthOfFileCont(void){
	return FileContent.size();
}
/// ===============================
void FileManager::PurgeFile(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str(), fstream::in | fstream::trunc);
	MyFile.close();
}
/// ===============================
void FileManager::WriteFile(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str(), fstream::out | fstream::app);
	try
	{
		for (int a = 1;true;a++)
		{
			MyFile << ReadFrFiCont(a);
			MyFile << endl;
		}
	}
	catch (int i)
	{
		if (i == 1)
		{
			return;
		}
	}
}
/// ===============================
void FileManager::GetFileCont(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str(), fstream::in);
	string TempLine;
	for (int a = 0;! MyFile.eof();a++)
	{
		getline(MyFile,TempLine);
		PutInFiCon(TempLine);
	}
	MyFile.close();
}
/// ===============================
void FileManager::PutInFiCon(string arg1)
{
	FileContent.push_back(arg1);
}
/// ===============================
string FileManager::ReadFrFiCont(int arg1)
{
	if (arg1 > FileContent.size())
	{
		throw 1;
	}
	return FileContent[arg1];
}
/// ===============================
bool FileManager::chkFile(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str());
	if (MyFile.is_open())
	{
		MyFile.close();
		return true;
	}
	MyFile.close();
	return false;
}
/// ===============================

void FileManager::SetFileName(string arg1)
{
	FileName = arg1;
}

string FileManager::SayFileName(void)
{
	return FileName;
}

string FileManager::SayFilePath(void)
{
	return FilePath;
}
/// ===============================
void FileManager::GetFileName(void)
{
	int LastMark = 0;
	string TrialFileName;
	for (int a = 0;true;a++)
	{
		if (a == FilePath.length() || a > FilePath.length())
		{
			break;
		}
		if (FilePath.at(a) == '/')
		{
			LastMark = a; // Marking last instance of "/"
		}
	}
	LastMark++;
	for (int a = LastMark;true;a++)
	{
		if (a == FilePath.length() || a > FilePath.length())
		{
			break;
		}
		TrialFileName.push_back(FilePath.at(a));
	}
	SetFileName(TrialFileName);
}
/// ===============================
#endif



TestMain.cpp:
// To be used to test ChkFile()
// To test GetFileName() and SetFileName()

#include <iostream>
#include <string>
#include <cstdlib>
#include "FileManager.h"

using namespace std;

int main(void)
{
	cout << "Enter File Path: ";
	string MyTmpStr;
	getline(cin,MyTmpStr);
	FileManager MyFLMan(MyTmpStr);
	if (MyFLMan.chkFile() != true)
	{
		cout << "FATAL! File: " << MyFLMan.SayFilePath() << " Not found. Exit();" << endl;
		cin.get();
		return 0;
	}
	MyFLMan.GetFileName();
	cout << "File Path is: " << MyFLMan.SayFilePath() << endl;
	cout << "File Name is: " << MyFLMan.SayFileName() << endl;
	MyFLMan.GetFileCont();
	for (int a = 0;a > MyFLMan.SayLengthOfFileCont();a++)
	{
		cout << MyFLMan.ReadFrFiCont(a) << endl;
	}
	cout << "End Of Buffer." << endl;
	cin.get();
	return 0;
}



My execution:
Enter File Path: /home/Delta/target.txt
File Path is: /home/Delta/target.txt
File Name is: target.txt
End Of Buffer.



content of target.txt:
Alpha 1 bravo.



Compiler: GNU GCC-C++
Debugger: GDB
OS: Linux
Environment Of Execution: BASH

Is This A Good Question/Topic? 0
  • +

Replies To: File Manager Class Logic Problems

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: File Manager Class Logic Problems

Posted 23 July 2009 - 07:18 AM

Your program has three major problems:

1. All the implementation code that you put into FileManager.h needs to either be moved to FileManager.cpp or made inline functions. If you try to use that header file in two or more *.cpp files then the linker will produce duplicate declaration errors because all those functions will be defined in each of the *.cpp files. Those functions are short enough that they can easily be inline, such as the two functions I added.

2. GetFilename() doesn't work if the name entered at the keyboard does not contain a path, but only a filename. I corrected the function. Also, since I'm testing on MS-Vista I made it recognize both *nix and Windows path separator characters -- '\' and '/'. You can just delete that feature if you want to.

3. In main(), the way to display the contents of a vector is by use of iterators.
	vector<string>::iterator it =  MyFLMan.getFileContentBegin();
	for(; it !=  MyFLMan.getFileContentEnd(); it++)
		cout << *it << "\n";



Here is how I changed the header file -- I made no attempt to convert the functions to inline. You might notice I made a couple of other very minor changes, such as using size_t instead of it because the return value of vector.size() and vector.length() is size_t (probably unsigned int on most systems)

// Trial Source Code for FileManager.h

// ** NOT FOR RELEASE **

#ifndef FILEMANAGER_H
#define FILEMANAGER_H

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>

using namespace std;

class FileManager
{
public:
	/// ** Member Stream Objects **
	fstream MyFile;

	/// ** Constructor **
	FileManager(string arg1);

	/// ** Member Functions : Public **
	bool chkFile(void); // Is The File Availible?
	void GetFileCont(void); // Read Contents of File
	void PurgeFile(void); // Empty File
	void WriteFile(void); // Write FileCont to file
	void PutInFiCon(string  arg1); // Put into FileCont
	string ReadFrFiCont(size_t arg1); // Read a line From FileCont
	void GetFileName(void); // Get Name of file from filePath
	void SetFileName(string arg1); // Sets FileName

	// Return Private Data's
	string SayFileName(void); // Returns FileName
	string SayFilePath(void); // Returns FilePath

	// Misc Functions
	int SayLengthOfFileCont(void);
	vector<string>::iterator getFileContentBegin()
	{
		return FileContent.begin();
	}
	vector<string>::iterator getFileContentEnd()
	{
		return FileContent.end();
	}

private:
	vector<string> FileContent;
	string  FileName;
	string  FilePath;
};
/// ===============================
FileManager::FileManager(string arg1)
{
	FilePath = arg1;
}

/// ===============================
int FileManager::SayLengthOfFileCont(void){
	return FileContent.size();
}
/// ===============================
void FileManager::PurgeFile(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str(), fstream::in | fstream::trunc);
	MyFile.close();
}
/// ===============================
void FileManager::WriteFile(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str(), fstream::out | fstream::app);
	try
	{
		for (int a = 1;true;a++)
		{
			MyFile << ReadFrFiCont(a);
			MyFile << endl;
		}
	}
	catch (int i)
	{
		if (i == 1)
		{
			return;
		}
	}
}
/// ===============================
void FileManager::GetFileCont(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str(), fstream::in);
	string TempLine;
	for (int a = 0;getline(MyFile,TempLine);a++)
	{
		PutInFiCon(TempLine);
	}
	MyFile.close();
	MyFile.clear();
}
/// ===============================
void FileManager::PutInFiCon(string arg1)
{
	FileContent.push_back(arg1);
}
/// ===============================
string FileManager::ReadFrFiCont(size_t arg1)
{
	if (arg1 > FileContent.size())
	{
		throw 1;
	}
	return FileContent[arg1];
}
/// ===============================
bool FileManager::chkFile(void)
{
	string MyTmpStr = SayFilePath();
	MyFile.open(MyTmpStr.c_str());
	if (MyFile.is_open())
	{
		MyFile.close();
		return true;
	}
	MyFile.close();
	return false;
}
/// ===============================

void FileManager::SetFileName(string arg1)
{
	FileName = arg1;
}

string FileManager::SayFileName(void)
{
	return FileName;
}

string FileManager::SayFilePath(void)
{
	return FilePath;
}
/// ===============================
void FileManager::GetFileName(void)
{
	string TrialFileName;
	size_t pos = FilePath.find('/');
	if( pos == string::npos )
		pos = FilePath.find('\\');
	if( pos != string::npos )
	{
		TrialFileName = FilePath.substr(0, pos);
	}
	else
		TrialFileName = FilePath;
	SetFileName(TrialFileName);
}
/// ===============================
#endif


This post has been edited by Ancient Dragon: 23 July 2009 - 07:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: File Manager Class Logic Problems

Posted 23 July 2009 - 07:27 AM

Thank You Ancient Dragon.
However, I have a question: On the fist item you addressed; why would the use of the header in multiple .cpp files cause a linker error? Aren't the statements:

#ifndef FILEMANAGER_H
#define FILEMANAGER_H
.
.
.
#endif



suppose to prevent that?

This post has been edited by Delta_Echo: 23 July 2009 - 07:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: File Manager Class Logic Problems

Posted 23 July 2009 - 07:40 AM

No -- those statements only allow you to include the same header file multiple times in the same *.cpp file, such as this
#include "FileManage.h"
#include "FileManage.h"


Was This Post Helpful? 1
  • +
  • -

#5 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: File Manager Class Logic Problems

Posted 23 July 2009 - 07:56 AM

Ah, i see. I think Im going to review my C++ reference for this information.
Thanks Ancient:) You Pwn!
Welcome to Dream in Code :)
Was This Post Helpful? 0
  • +
  • -

#6 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: File Manager Class Logic Problems

Posted 27 July 2009 - 09:05 PM

In GetFileName(), what is string::npos? How are you accessing a variable that is a member of an undeclared container type?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1