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

New Topic/Question
Reply




MultiQuote




|