/* SaveMap overwrites the data file with a current version of the map */ void DATABASE::SaveMap(void) { string temp; char* cstr; ofstream file; file.open(IOFILE,ios::trunc); if (file.eof()) cout<<"eofbit set"<<endl; if (file.fail()) cout<<"failbit set"<<endl; if (file.bad()) cout<<"badbit set"<<endl; if (file.fail()) { cout<<"\nFile read\\write failed: SaveMap()\n"; exit(777); } for (iter = mymap.begin(); iter != mymap.end(); ++iter) { temp = iter->second; // converting string to array cstr = new char [temp.size()+1]; strcpy(cstr,temp.c_str()); cstr[temp.size()]='\n'; file.write(cstr,temp.size()+1); delete cstr; } file.close(); cout<<"File saved."<<endl; }
void showState(fstream &file) { cout << "File Status:\n"; cout << " eof bit: " << file.eof() << endl; cout << " fail bit: " << file.fail() << endl; cout << " bad bit: " << file.bad() << endl; cout << " good bit: " << file.good() << endl; file.clear(); // Clear any bad bits }
void openFile(fstream &file, const string &fileName, const std::ios_base::openmode openmode) throw (string) { file.open(fileName.c_str(), openmode); if (file.bad()) { if (file.is_open()) { file.close(); } string message = "Nie udalo sie otworzyc pliku \"" + fileName + "\""; throw message; } }
int CSndBuffer::addBufferFromFile(fstream& ifs, int len) { int size = len / m_iMSS; if ((len % m_iMSS) != 0) size ++; // dynamically increase sender buffer while (size + m_iCount >= m_iSize) increase(); Block* s = m_pLastBlock; int total = 0; for (int i = 0; i < size; ++ i) { if (ifs.bad() || ifs.fail() || ifs.eof()) break; size_t pktlen = len - i * m_iMSS; if (pktlen > m_iMSS) pktlen = m_iMSS; ifs.read(s->m_pcData, pktlen); pktlen = ifs.gcount(); if (pktlen <= 0) break; // currently file transfer is only available in streaming mode, message is always in order, ttl = infinite s->m_iMsgNo = m_iNextMsgNo | 0x20000000; if (i == 0) s->m_iMsgNo |= 0x80000000; if (i == size - 1) s->m_iMsgNo |= 0x40000000; s->m_iLength = pktlen; s->m_iTTL = -1; s = s->m_pNext; total += pktlen; } m_pLastBlock = s; CGuard::enterCS(m_BufLock); m_iCount += size; CGuard::leaveCS(m_BufLock); m_iNextMsgNo ++; if (m_iNextMsgNo == CMsgNo::m_iMaxMsgNo) m_iNextMsgNo = 1; return total; }
void WriteCollection(fstream &out,MyIndexCollection &col,int Distinct=0)//,FieldInd) { int SizeStruct=col.GetSizeElement(); int NumRead=col.NumElement(),*IndexAr=col.IndexAr; int k=1; while (k<=NumRead) { char *buf=(char*)col.GetElement(IndexAr[k]); out.write(buf,SizeStruct); k++; if (Distinct) { int beg=k-1;while ( (k<=NumRead) && (Order(IndexAr[beg],IndexAr[k])==0) ) k++;} if (out.bad()) {ErrorOut<<" Error writing file.";throw int();}; } };
int check_flags(const fstream& fh) { int status = 0; //cout << "file is:\n"; if (fh.good()) status+=16; if (fh.bad()) status+=2; if (fh.fail()) status+=4; if (fh.eof()) status+=8; if (0 == status) status = -1; return status; }
//这个函数存在问题,不能够处理大于2G的文件。 int CSndBuffer::addBufferFromFile(fstream& ifs, const int& len) { int size = len / m_iMSS; if ((len % m_iMSS) != 0) size ++; // dynamically increase sender buffer while (size + m_iCount >= m_iSize) increase(); Block* s = m_pLastBlock; int total = 0; for (int i = 0; i < size; ++ i) { if (ifs.bad() || ifs.fail() || ifs.eof()) break; int pktlen = len - i * m_iMSS; if (pktlen > m_iMSS) pktlen = m_iMSS; ifs.read(s->m_pcData, pktlen); if ((pktlen = ifs.gcount()) <= 0) break; s->m_iLength = pktlen; s->m_iTTL = -1; s = s->m_pNext; total += pktlen; } m_pLastBlock = s; CGuard::enterCS(m_BufLock); m_iCount += size; CGuard::leaveCS(m_BufLock); return total; }