Failure in file creation and exception raised

S

s_amrollahi

Dear All

Hello
I am Saeed Amrollahi.
I encountered a very strange bug in my program. The problem is:
the program reads a formatted text file of trade summary in Tehran
Stock Exchange organization. In each line there are same fields like
Date (YYYYMMDD) Ticker Symbol Vol of Share Price ...
Based on date, I should check is there the file with the full path:
<Drive Letter>\StockExchangeDataCenter\TradeSummary\YYYY\MM\DD
if the file exists, I should append the line to the file and if there
isn't a file with above path, the program creates an empty file in
suitable path. An exception raised when
program want to create an empty file.
I am sure the path is correct, but I think Windows are sensible about
the depth of path. another part of program uses the following path:
<Drive Letter>\StockExchangeDataCenter\TradeSummary\YYYY
and it is OK!
I know my question may be an Off topic one, but please help me.
my code is here (the unrelated details were excluded):

void CAS400SysTrdSummaryInsDialog::InsRec(const string& s) // insert
string into data center's file
{
string ye;
ye = "2006";
string mo;
mo = "11";
string da;
da = "12";
// create correct path
map<string, string> m = g_DataCenterManager;
string PathName = DataCenterManager::DataCenterPath +
m["TRD_SUMMARY_CAT"] + m["AS400_DATA_SOURCE"] + ye + "mo + da +
".WIN";
CFileFind Finder;
BOOL b = Finder.FindFile(PathName.c_str());
if (!b) { // a file with <Year>.WIN should be created
AS400OutFile f(PathName); // <----- Exception raised!!!

try {
if (!f)
throw FileIsNotCreated();
}

catch(const FileIsNotCreated&) {

}

f.Close(); // closing file
/* ... */
}

else { // the file already have been created
AS400OutFile f(PathName, AS400OutFile::APPEND);
/* ... */
}
}


class AS400OutFile {
public:
enum OPEN_MODE { OUTPUT, APPEND };
AS400OutFile(std::string&, OPEN_MODE = OUTPUT); // use it for
creating and opening a file (for wrting into an AS/400 source file)
void Open(std::string&, OPEN_MODE = OUTPUT);
void Close();
~AS400OutFile();
// ...
private:
std::string PathName; // full pathname
std::eek:fstream f; // stream file
};

inline AS400OutFile::AS400OutFile(std::string& PathName_, OPEN_MODE
m) :
PathName(PathName_)
{
if (m == OUTPUT)
f.open(PathName.c_str(), ios_base::eek:ut); // open for write
else
f.open(PathName.c_str(), ios_base::app); // open for append

if (!f)
throw FileIsNotCreated(PathName);
}

As a final word, I learned from chapter 34 of C++ FAQ Lite:
Miscellaneous technical issues: "You should use forward slashes ("/")
rather than backslashes ("\") in your
#include filenames, even on an operating system that uses backslashes
such as DOS, Windows".
I use Visual C++ 6.0 under Windows XP.

Regards
 
H

Howard

Dear All

Hello
I am Saeed Amrollahi.
I encountered a very strange bug in my program. The problem is:
the program reads a formatted text file of trade summary in Tehran
Stock Exchange organization. In each line there are same fields like
Date (YYYYMMDD) Ticker Symbol Vol of Share Price ...
Based on date, I should check is there the file with the full path:
<Drive Letter>\StockExchangeDataCenter\TradeSummary\YYYY\MM\DD
if the file exists, I should append the line to the file and if there
isn't a file with above path, the program creates an empty file in
suitable path. An exception raised when
program want to create an empty file.
I am sure the path is correct, but I think Windows are sensible about
the depth of path. another part of program uses the following path:
<Drive Letter>\StockExchangeDataCenter\TradeSummary\YYYY
and it is OK!
I know my question may be an Off topic one, but please help me.
my code is here (the unrelated details were excluded):

Perhaps the directory does not exist? You can't create a file in a
directory unless the directory has been created first. (And for information
on creating directories, you need to ask on a Windows newsgroup.)

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top