Click to See Complete Forum and Search --> : c++ help


crokett
09-12-2001, 12:03 PM
as written this method traps when it is done running - the destructor traps when it tries to clean up. the code in bold is the offending code, but when I move it to the bottom of the method the program runs with no problem. i am wondering if it is the compiler that is the problem since it shouldn't matter where in the program the code is. i tried flushing the stream but got the same problem. i don't want to simply get it working, i want to figure out what is wrong. i also thought about using a single stream since i am closing each one anyway, but again i don't want to cover up the problem i want to know why. incase it is the compiler, right now i am using VisualC++, though I am also porting it to *nix. thanks for any assistance and you guys always give me better and faster help than the MS forums.

//routine that sets up create drive image scripts
void createImage(char imgDrv, char ftpServer[], char user[], char passwd[], string remotePath,
string remoteFile, string localFile){

char pqdiResponse[200];
pqdiResponse[0]=imgDrv;
pqdiResponse[1]='\0';
strcat(pqdiResponse, ":\\create.txt");

ofstream createtxt;
createtxt.open(pqdiResponse,ios: :out);
createtxt << "SELECT DRIVE 1" << endl;
createtxt << "SELECT PARTITION 2" << endl;
createtxt << "STORE WITH COMPRESSION HIGH" << endl;
createtxt.close();
printf("created PQDI file!\n");


char dgFile[12];
dgFile[0]=imgDrv;
dgFile[1]='\0';
strcat(dgFile,":\\dosgo.bat");

// create dosgo filestream
ofstream dosgo;
// open for ouput on dosgo.bat
dosgo.open(dgFile,ios: :out);
printf("Create image ran!\n");
dosgo << "c:\\pqdi.exe /cmd=%IMGDRV%:\\create.txt /img=%IMGDRV%:\\"
<< imgFile << " /I24 /IFC /NBS /NRB /ERR=%IMGDRV%:\\create.err /LOG=%IMGDRV%:\\create.log" << endl;
dosgo << "if exist %IMGDRV%:\\create.err goto IMGERR" << endl;
dosgo << "call %IMGDRV%:\\errorlog.bat 000 \"Created Image, restarting to Windows\" %IMGDRV%"<< endl;
dosgo << "goto DONE" << endl;
dosgo << ":IMGERR" << endl;
dosgo << "call %IMGDRV%:\\errorlog.bat 851 \"Error creating image file\" %IMGDRV%" << endl;
dosgo << " :DONE" << endl;
dosgo << "call c:\\setpdos.bat" << endl;
dosgo << "call c:\\setactiv.bat" << endl;
// dosgo.flush(); - you have to flush if you don't endl
dosgo.close();
printf("created dosgo file\n");

// the ftpput filestream to put image file
char ftpFile[13];
ftpFile[0]=imgDrv;
ftpFile[1]='\0';
strcat(ftpFile, ":\\ftpput.bat");

ofstream ftpput;
ftpput.open(ftpFile,ios: :out);

ftpput << "if exist " << imgDrv << ":\\ftpdoflg.flg goto :NOFTP" << endl;
ftpput << "c:\\ibm\\iaaconfig\\getfile " << ftpServer << " "
<< user << " " << passwd << " " << remotePath << " " << imgDrv
<< " " << remoteFile << " " << localFile << endl;
ftpput << "if errorlevel 1 goto FTPFAIL" << endl;
ftpput << "call " << imgDrv << ":\\errorlog.bat 000 \"Created file on FTP Server\""
<< imgDrv << endl;
ftpput << "call " << imgDrv << ":\\errorlog.bat 111 \"Create Image Complete\""
<< imgDrv << endl;
ftpput << "goto EXIT" << endl;
ftpput << ":FTPFAIL" << endl;
ftpput << "call " << imgDrv << ":\\errorlog.bat 805 \"Error creating file on FTP Server\" "
<< imgDrv << endl;
ftpput << "goto EXIT" << endl;
ftpput << ":NOFTP" << endl;
ftpput << "call " << imgDrv << ":\\errorlog.bat 000 \"Image created on this machine so not FTPing file up\" "
<< imgDrv << endl;
ftpput << ":EXIT" << endl;
ftpput << "call c:\\ibm\\iaaconfig\\clean.bat c:\\ibm\\iaaconfig "
<< imgDrv << endl;
ftpput.close();
printf("created dftp file\n");
// the autoexnt filestream for autoexnt.bat
string exntBuf = systemDir + "\\autoexnt.bat";
char* autoEXNTBat = new char[exntBuf.length()+1];
exntBuf.copy(autoEXNTBat, exntBuf.length());
*(autoEXNTBat + exntBuf.length()) = 0;

// create autoexnt.bat file
ofstream autoexntfile;
autoexntfile.open(autoEXNTBat,ios: :out);
autoexntfile << "if exist " << rlFlagStr << " call " << installDir
<< "\\restlogic.bat" << endl;
autoexntfile << "if exist " << ftpflag << " call " << installDir
<< "\\ftpput.bat" << endl;
autoexntfile.close();
printf("created autont file\n");
// now create flag files to enable restlogic operation
char* dorlFlg = new char[rlFlagStr.length()+1];
rlFlagStr.copy(dorlFlg, rlFlagStr.length());
*(dorlFlg + rlFlagStr.length()) = 0;

ofstream dorlFile;
dorlFile.open(dorlFlg,ios: :out);
dorlFile << "running restlogic" << endl;
dorlFile.close();
printf("created flag file\n");


}

crokett
09-12-2001, 02:18 PM
it appears to be a compiler error. I changed the code to use a singe ofstream for all the files, that worked. I then tried using pointers and separate file streams, it gave the same error when I tried to deallocate all the pointers at the end. if I deleted each one when I was done with it, that works.

TheLinuxDuck
09-12-2001, 02:24 PM
One thing that I noticed, although I am no c++ expert, is that once the stream has been opened, there are no verifications or checks to make sure that it is opened as it should be. May be related, may not be.

Dunno.

jemfinch
09-12-2001, 04:28 PM
Someone should die a long, slow, painful death for the creation of "Hungarian Notation."

Jeremy

Stuka
09-12-2001, 05:11 PM
TLD - a simple if(*stream variable*) will test to see if the stream opened properly - so a sequence like this:ofstream outfile("somefile");
if (!outfile) { //Handle file open error here} will do proper error checking.

sans-hubris
09-13-2001, 12:41 PM
It has to do with you using the string::copy function on cstrings. I don't exactly know the details and I'm hungry right now, but I'll get back here on it.

sans-hubris
09-15-2001, 02:18 PM
Oops, it has nothing to do with the string::copy function.
Actually, you didn't free up all the memory at the end of the function,
in particular, the char* arrays you made.
It seems that g++ is storing all the local variables for this
function on the stack and so when you use dynamic arrays like you did
and don't free up that memory before exiting the function,
you will probably mess up the stack and it won't be
able to destroy some of the objects properly because it can't
find their memory address. Free up (use delete) that memory before exiting and hopefully that should fix things.