Message-Id: <200302140432.h1E4WcuJ018929@chac.its.uow.edu.au> Date: Fri, 14 Feb 2003 15:35:47 +1000 From: Y Chen To: "djgpp AT delorie DOT com" Subject: open() in c++ X-mailer: FoxMail 3.11 Release [cn] Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: djgpp AT delorie DOT com Dear Sir, I've changed char TIOBase::OpenFile(char* acFilename, int aiMode) to char TIOBase::OpenFile(char* acFilename, std::_Ios_Openmode aiMode) to avoid the error: IOBase.cpp:45: no matching function for call to `std::basic_fstream >::open(char[101], int&)' But the following sentence: if (OpenFile(cParFilename, ios::in)) return 1; gives the following error during link: LSTM2.o(.text+0x11f6):LSTM2.cpp: more undefined references to `TIOBase::OpenFile (char*, int)' follow How to modify ios::in to get rid of the link error? Thanks a lot for your help. Regards, Y Chen //IOBase.cpp #include #include #include #include #include #include #include // e.g. sleep #include // struct termios //#include //#include #include "IOBase.h" ///////////////////TIOBase TIOBase::TIOBase() { pFile = new fstream(); } TIOBase::~TIOBase() { delete pFile; // callls fstream::close } char TIOBase::OpenFile(char* acFilename, std::_Ios_Openmode aiMode) { // The calling function should take care of closing pFile, e.g // with CloseFile(). But we check if it is open. #ifdef DEBUG cerr << " opening... " << acFilename << " rdstate: " << pFile->rdstate() << endl; #endif if(!pFile) cerr << "pFile doesn't exist!" << endl; pFile->tellg(); // try to access it and wait for error. #ifdef DEBUG cerr << " Check if " << acFilename << " is already in use." << endl; #endif if(!pFile->fail()) { cerr << "Warning: A file was already open (old file): " << acFilename << " (new file): " << acFilename; // So we clear and close it. pFile->clear(); pFile->close(); } // keep the actual filename for messages strcpy(cFilename, acFilename); // open the file. One can add an other parameter for the file-rights pFile->open(cFilename, aiMode); if(!pFile->good()) { cerr << " File not good : " << cFilename << " !\n"; pFile->clear(); pFile->close(); return 1; } #ifdef DEBUG cerr << cFilename << " open.\n"; #endif return 0; } char TIOBase::CloseFile() { // check file. We use the ios statefalgs. iFileRdState = pFile->rdstate(); // Also a EOF should not happen, it indicates that a file is not complete. if(!pFile->good()) { // && !pFile->eof()) { // !pFile->fail cerr << "File error before closing in " << cFilename << "\n"; iFileRdState = 1; } // Clear in case of error and close file pFile->close(); #ifdef DEBUG cerr << "\n" << cFilename << " closed regularly.\n"; #endif strcpy(cFilename,""); if(iFileRdState) return 1; else return 0; } char TIOBase::ReadComment() { // skip comment (normally at the beginning of a file) if(!pFile) { cerr << "No File " << cFilename << " to read comment from.\n"; return 1; } *pFile >> c; while((c == '#' || c == ' ' || c == '\n') && pFile->good()) { #ifdef DEBUG cerr << c; #endif if(c == '#') if(c == '#') pFile->ignore(1000, '\n'); *pFile >> c; } pFile->putback(c); if(pFile->eof()) { cerr << "EOF while reading comment from " << cFilename << "\n"; return 1; } if(pFile->fail()) { cerr << "FAIL while reading comment from " << cFilename << "\n"; return 1; } #ifdef DEBUG cerr << "\nComment read.\n"; #endif return 0; } char TIOBase::OpenFile(fstream *apFile, char* acFilename, std::_Ios_Openmode aiMode) { // The calling function should take care of closing apFile, e.g // with CloseFile(). But we check if it is open. #ifdef DEBUG cerr << " opening... " << acFilename << " rdstate: " << apFile->rdstate() << endl; #endif if(!apFile) cerr << "apFile doesn't exist!" << endl; apFile->tellg(); // try to access it and wait for error. #ifdef DEBUG cerr << " Check if " << acFilename << " is already in use." << endl; #endif if(apFile->fail()) { cerr << "Warning: A file was already open (old file): " << acFilename << " (new file): " << acFilename; // So we clear and close it. apFile->clear(); apFile->close(); } // open the file. One can add an other parameter for the file-rights apFile->open(acFilename, aiMode); if(!apFile->good()) { cerr << " File not good : " << acFilename << " !\n"; apFile->clear(); apFile->close(); return 1; } #ifdef DEBUG cerr << acFilename << " open.\n"; #endif return 0; } char TIOBase::CloseFile(fstream *apFile) { // check file. We use the ios statefalgs. iFileRdState = apFile->rdstate(); // Also a EOF should not happen, it indicates that a file is not complete. if(!apFile->good()) { // && !apFile->eof()) { // !apFile->fail cerr << "File error before closing in " << "\n"; iFileRdState = 1; } // Clear in case of error and close file apFile->close(); #ifdef DEBUG cerr << "\n" << " closed regularly.\n"; #endif if(iFileRdState) return 1; else return 0; } bool TIOBase::DirContainsFile(bool SubSearch, char *fn) { DIR *dir; struct dirent *dr; dir = opendir("."); while(dr = readdir(dir)) { if(SubSearch) { if(strstr(dr->d_name, fn)) { closedir(dir); return 1; } } else if(!strcmp(fn, dr->d_name)) { closedir(dir); return 1; } } closedir(dir); return 0; } int TIOBase::Keypressed(char &ac) { struct termios term, termsave; // We are not always in noncanonical mode without local echo, so if // a key is pressed "outside" this function (which is likely) // it will be echoed. // tty_noncanonical(STDIN_FILENO); if (tcgetattr(STDIN_FILENO, &termsave) < 0) cerr << "get error" << endl; term = termsave; term.c_lflag &= ~(ICANON | ECHO ); // noncanonical and no echo term.c_cc[VMIN] = 0; // buffer size term.c_cc[VTIME] = 0; if (tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) cerr << "set error" << endl; // Check for keypressed int iReturnValue = read(STDIN_FILENO, &ac, 1); // tty_reset(STDIN_FILENO); if (tcsetattr(STDIN_FILENO, TCSANOW, &termsave) < 0) cerr << "set error" << endl; // Done. Return nuber of chars read. return iReturnValue; } //IOBase.h #include /*** defines ***/ // switch for all debugging info //#define DEBUG /*** classes in this header ***/ class TIOBase; ///////////////////TTIOBase // a container class for trival common variables and functions. // Handle the fileIO. class TIOBase { public: // We leave the standart constructor empty, and do all the initializations // in the other one. The empty one is necessairy for TTopoCreator and // TPatternManager, becase their constructors are empty too. But as thier // classes exist only virtually for TRNN, the empty constructor of // TIOBase will never be called. So no one will realize that it doesn't // do its work properly. TIOBase(); ~TIOBase(); protected: // possible Mode parameters are defined in clsss ios (ios::out,in,...) // The childs are responsible closing and deleting the file again. char OpenFile(char* acFilename, std::_Ios_Openmode aiMode); char OpenFile(fstream *apFile, char* acFilename, std::_Ios_Openmode aiMode); char CloseFile(); char CloseFile(fstream *apFile); char ReadComment(); bool DirContainsFile(bool SubSearch, char *fn); int Keypressed(char &ac); // Double to stream and the other way round. static void d2s(iostream *s, double &d) { *s << d; } static void s2d(iostream *s, double &d) { *s >> d; } // For long doubles. static void ld2s(iostream *s, long double &d) { double dd; dd=(double)d; *s << dd; } static void s2ld(iostream *s, long double &d) { double dd; *s >> dd; d=dd; } // We keep only one fstream object alive and open - close the files // instead of new - delete an fstream object every time we read a file. fstream *pFile; // common stream pointer for all childs long lFilePos; // to rescan the file begining after the global section char c; // to read comments private: char cFilename[101]; int iFileRdState; // the status of the file. };