Archived

This topic is now archived and is closed to further replies.

ShadowWolf

Object-Oriented File I/O

Recommended Posts

ShadowWolf    154
What I''m using: C++ in Microsoft Visual Stuido .NET Multiple Files (such as Maze.h/cpp, CApplication.h/cpp, etc.) STL I''m having a very strange problem with file I/O right now. I''m using streams, and I''m doing something like this:
Class FileIO {
public:
// Open stream opens my file up
// I have it set statically for now.
BOOL OpenFile();
// WriteFile actually writes to the file
BOOL WriteFile(char* szWriteMe);
private:
std::ofstream m_fWriteFile;
}

Class CApplication {
private:
FileIO m_cFileIO
}

BOOL FileIO::OpenFile() { 
m_fWriteFile.open("T:\\DEBUG.TXT");
if (m_fWriteFile.good()) { return true; }
}

BOOL FileIO::WriteFile(char* szWriteMe) {
using namespace std;
if(m_fWriteFile.good() && szWriteMe != NULL)
{
   m_fWriteFile << szWriteMe << endl;
}
else
{
   MessageBox(NULL,"ERROR IN FILE WRITE","ERROR!",MB_OK);
}
};
 
Those are (essentially) the relevant parts of my class definitions. They do other things, but those are irrelevant. The application itself calls the File IO for Debugging purposes only. Essentially, rather than try to convert things through try/catch, I just drop them to the debug file. It does work ok, unless I drop to another file with classes. For example, if I call something like `CApplication::GetInstance()->WriteError("Something goes here");`, then I get potential error in acess: violation in in the Visual Studio .NET Debugger, and the good() check in WriteFile() is FALSE when it should be TRUE. Any ideas as to what I can do, and more importantly, why this is happening? It doesn''t seem to happen 100% of the time either...

Share this post


Link to post
Share on other sites
MonkeyChuff    158
Your code ... just changed a few bits ... all works ok ...

StreamClasses.h

#include <fstream>
#include <string>

class CFileIO
{
public:
CFileIO();
~CFileIO();

bool OpenFile();
bool WriteFile(std::string& sMessage);
private:
std::ofstream m_oFileStream;
};

class CApplication
{
public:
CApplication();
~CApplication();
void WriteError(std::string& sError);
private:
CFileIO* m_oFileIO;
};


StreamClasses.cpp

#include "stdafx.h"
#include "StreamClasses.h"

CFileIO::CFileIO()
{
m_oFileStream.open("c:\\temp\\afile.txt", std::ios::out | std::ios::app);
}

CFileIO::~CFileIO()
{
if (m_oFileStream.good())
{
m_oFileStream.close();
}
}

bool CFileIO::OpenFile()
{

return true;
}

bool CFileIO::WriteFile(std::string& sMessage)
{
if (m_oFileStream)
{
m_oFileStream.write(sMessage.c_str(), sMessage.length());
return true;
}

return false;
}

CApplication::CApplication()
{
m_oFileIO = new CFileIO();
}

CApplication::~CApplication()
{
if (m_oFileIO)
{
delete m_oFileIO;
}
}

void CApplication::WriteError(std::string& sError)
{
if (m_oFileIO)
{
m_oFileIO->WriteFile(sError);
}
}


The Console App ...

#include "stdafx.h"
#include "StreamClasses.h"

int _tmain(int argc, _TCHAR* argv[])
{
std::string sMessage = "This is the error";
CApplication* oApplication = new CApplication();
oApplication->WriteError(sMessage);
delete oApplication;
return 0;
}


Hope this helps ...

Edit ... Altered the tags


[edited by - MonkeyChuff on August 15, 2003 4:41:48 AM]

Share this post


Link to post
Share on other sites
ShadowWolf    154
Actually, that didn''t help at all. Setting those two IOS bits isn''t necessary since the defaults work just fine.

The problem is actually my usage of the good() method. It won''t work, but switching it to bad() actually does work. Not only does it catch more errors (so I''m told, I don''t know about that), but the more important thing is that my usage of the good() method is improper.

So, just for anyone''s information, where it says:


if(m_fWriteFile.good() && szWriteMe != NULL)
{
m_fWriteFile << szWriteMe << endl;
}else
{
MessageBox(NULL,"ERROR IN FILE WRITE","ERROR!",MB_OK);
}
};


It should, instead, read:

if(m_fWriteFile.bad() || szWriteMe == NULL)
{
MessageBox(NULL,"ERROR IN FILE WRITE","ERROR!",MB_OK);
}
else
{
m_fWriteFile << szWriteMe << endl;
}


Such a foolish mistake on my part. I should''ve just checked my book on the STL...

Share this post


Link to post
Share on other sites
Greatwolf    125
So are you saying that, basically, just because a fileIO is not good doesn''t automatically mean it''s bad? or what''s the story here?





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
ShadowWolf    154
Well, no. The way it works, the good() method checks to see if any rdstate bits are set (bad, eof, fail, etc.). I''m not sure why some bits in my program are being set, but they''re not relevant to anything going on. All I need to know is whether the bad bit is being set.

Perhaps someone else can explain this better to clarify, this sorta stuff is kinda confusing when you''re not too familiar with the STL.

Share this post


Link to post
Share on other sites