void PrimaryIndex::add(fstream& primary){ int x = fnd(AccountNumber, primary); if(x < 0){ x = -(x + sizeof(PrimaryIndex)); primary.clear(); primary.seekg(x); PrimaryIndex m(0, -5); primary.read(reinterpret_cast<char*>(&m), sizeof(PrimaryIndex)); primary.clear(); if(m.Byte > -1)sft(x, primary); primary.clear(); primary.seekp(x); primary.write(reinterpret_cast<char*>(this), sizeof(PrimaryIndex)); } }
void writeDIff(fstream & statStream){ statStream.clear(); if(!isEmpty(statStream)) statStream<<"}"; statStream.clear(); for(int i = 0;i<filesDiff.size();i++){ statStream<<filesDiff.at(i).first<<"}"<<filesDiff.at(i).second; if(i != filesDiff.size() - 1) statStream<<"}"; } statStream.close(); if(filesDiff.size()) cout<<"Difference Written"<<endl; }
Boolean PrimesTable::TableBuffer::RetrieveBlock(Ulong Index_, long Offset_, fstream& Fin_) { Ulong BlockNum_ = Index_ / PrimesTable::TABLESIZE; Fin_.seekg(0, ios::end); Fin_.clear(); long StartPos_ = BlockNum_ * TABLESIZE * sizeof(long); long Fsize_ = Fin_.tellg(); Fsize_ -= Offset_; if (StartPos_ < Fsize_) if (Fin_.seekg(StartPos_ + Offset_).good()) { Fin_.read((char*)_Table, TABLESIZE * sizeof(long)); size_t i; long BufSize_ = TABLESIZE; for (i = TABLESIZE - 1; i != size_t(-1); --i) if (!_Table[i]) --BufSize_; else break; if (BufSize_) { StartPos_ /= sizeof(long); SetLimit(StartPos_, BufSize_); return Fin_.good(); } } return FALSE; }
// return nabber of filled objects int fillFromFile(fstream & fin, employee ** pArr, int nSize) { int id; int i = 0; while (i < nSize && fin.good() && fin.peek() != EOF) { fin >> id; switch(id) { case abstr_emp::EMPLOYEE: pArr[i] = new employee; break; case abstr_emp::MANAGER: pArr[i] = new manager; break; case abstr_emp::FINK: pArr[i] = new fink; break; case abstr_emp::HIGHFINK: pArr[i] = new highfink; break; default: throw "Error while reading file"; } fin.get(); // '\n' pArr[i]->GetAll(fin); i++; } fin.clear(); return i; }
//Synchronize and saves map with frequency numbers in file void saveFrequencyInFile(fstream &write, FrequencyNumber &freq) { map<int, int> temp2; map<int, int>::iterator it; int key, value; char ch; while (write >> key >> ch >> value){ temp2[key] = value; } map<int, int> temp = freq.getMap(); temp.insert(temp2.begin(), temp2.end()); write.clear(); write.seekp(0); it = temp.begin(); for (int i = 0; i < 80; i++) { if ((i + 1) == it->first) { write << it->first << "-" << it->second << endl; it++; } else{ write << i + 1 << "-" << 0 << endl; } } }
int ImageType::getPositionAfterHeader(fstream& image) { image.clear(); image.seekg(0); char line[COMMENT_LENGTH]; char c; image.getline(line, COMMENT_LENGTH); image.get(c); while (c == '#') //preskacham komentarite { image.getline(line, COMMENT_LENGTH); image.get(c); } image.unget(); int skippedNumber; image >> skippedNumber; //preskacham width image >> skippedNumber; //preskacham heigth image >> skippedNumber; //preskacham maxValue image.getline(line, COMMENT_LENGTH); return image.tellg(); }
bool CCoder::decodeRLE( fstream& in, CFastOStream& out ) { uint32_t blockSize = 0; uint32_t lastBlockSize = 0; uint32_t blockCount = 0; in.seekg( -12, in.end ); logger.setFileSize( (size_t) in.tellg() + 12 ); in.read( (char *) &blockSize, 4 ); in.read( (char *) &lastBlockSize, 4 ); in.read( (char *) &blockCount, 4 ); in.seekg( -12 - (int64_t)blockCount * 4, in.end ); vector<uint32_t> header; header.resize( blockCount, 0 ); in.read( (char *) &header.front(), blockCount * 4 ); if ( in.fail() ) return false; logger.updateInput( blockCount * 4 + 12 ); in.clear(); in.seekg( 0, in.beg ); CFISFromStream inStream( in ); return decodeRLE( inStream, out, header, blockSize, lastBlockSize, blockCount ); }
//option 8 void WriteTextArchiveAndDeleteBinary ( fstream& ioFile, bool& successful ) { ioFile.clear(); string outputFile; ReadThisLine("Enter name of output textfile for archiving " "club member data: ",outputFile); ofstream outFile(outputFile); if(!outFile) { cout << "\nError opening out textfile.\nReturning to menu." << endl; Pause(); successful = false; } else if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { //start at beginning of file ioFile.seekg(0); while(!ioFile.eof()) { //read the binary file ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //store the info from binary file back into a txt file outFile << member.name << endl; outFile << member.age << " $" << member.balance << endl; } } successful = true; //clear the txtfile as well as the binary file outFile.clear(); outFile.close(); ioFile.clear(); ioFile.close(); //delete the binary file remove("working.bin"); } }
void File::QSort(int low, int high) { int pivotLoc; if (low < high) { outfile.clear(); partition(low, high, pivotLoc); QSort(low, pivotLoc-1); QSort(pivotLoc+1, high); } }
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 }
// ====== FUNCTION DEFINITION ====== // change a record specified by the user void chngRec(Inventory &product, fstream &fileName, int records) { fileName.clear(); // clear flags int recCh; // record to change cout << "\n\tChange a record\n"; cout << "You have " << records << " records" << endl; cout << "Which record would you like to change? "; cin >> recCh; cin.ignore(); // ignore the newline char --recCh; // minus 1 to seek properly // seek to the record fileName.seekg(sizeof(product) * recCh, ios::beg); // let the user change the record cout << "Changing record #" << (recCh + 1) << endl << endl; cout << "Enter the following data about a product:\n"; cout << "Item Description: "; cin.getline(product.descr, DES_SIZE); do { cout << "Quantity on hand: "; cin >> product.quantity; cin.ignore(); // ignore the newline char } while (product.quantity < 0); do { cout << "Wholesale cost: $"; cin >> product.wholesaleC; cin.ignore(); // ignore the newline char } while (product.wholesaleC < 0); do { cout << "Retail cost: $"; cin >> product.retailC; cin.ignore(); // ignore the newline char } while (product.retailC < 0); do{ cout << "Date added to inventory (MM/DD/YYYY): "; cin.getline(product.dateAdded, DATE_SIZE); } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5]))); // rewrite over record in the file fileName.write(reinterpret_cast<char *>(&product), sizeof(product)); // seek the file back to the beginning fileName.seekg(0L, ios::end); cout << "The file has been rewritten\n"; }
// ====== FUNCTION DEFINITION ====== // user adds new records. keeps track of total number of records void newRecs(Inventory &product,fstream &fileName, int &records) { fileName.clear(); // clear flags int numRecordAdded = 0; // number of new records added char anotherR; // To hold Y or N // go to the end of the file to append the new record fileName.seekg(0L, ios::end); cout << "\n\tAdd new records\n"; do { cout << "Enter the following data about a product:\n"; cout << "Item Description: "; cin.getline(product.descr, DES_SIZE); do { cout << "Quantity on hand: "; cin >> product.quantity; cin.ignore(); // ignore the newline char } while (product.quantity < 0); do { cout << "Wholesale cost: $"; cin >> product.wholesaleC; cin.ignore(); // ignore the newline char } while (product.wholesaleC < 0); do { cout << "Retail cost: $"; cin >> product.retailC; cin.ignore(); // ignore the newline char } while (product.retailC < 0); do{ cout << "Date added to inventory (MM/DD/YYYY): "; cin.getline(product.dateAdded, DATE_SIZE); } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5]))); // write the contents of the product structure to the file fileName.write(reinterpret_cast<char *>(&product), sizeof(product)); // determine if user wants to write another record cout << "Do you want to write another record? (y for yes)\n"; cin >> anotherR; cin.ignore(); // ignore the newline char ++numRecordAdded; } while (anotherR == 'Y' || anotherR == 'y'); records += numRecordAdded; }
void File::partition (int low, int high, int& pivotLoc) { char rec[recordLen+1], pivot[recordLen+1]; register int i, lastSmall; swap(pointers[low],pointers[(low+high)/2]); outfile.clear(); outfile.seekg(pointers[low]*recordLen,ios::beg); outfile.get(pivot,recordLen+1); for (lastSmall = low, i = low+1; i <= high; i++) { outfile.clear(); outfile.seekg(pointers[i]*recordLen,ios::beg); outfile.get(rec,recordLen+1); if (strcmp(rec,pivot) < 0) { lastSmall++; swap(pointers[lastSmall],pointers[i]); } } swap(pointers[low],pointers[lastSmall]); pivotLoc = lastSmall; }
void ReadAlignChunk::chunkFstreamCat (fstream &chunkOut, ofstream &allOut, bool mutexFlag, pthread_mutex_t &mutexVal){ chunkOut.flush(); chunkOut.seekg(0,ios::beg); if (mutexFlag) pthread_mutex_lock(&mutexVal); allOut << chunkOut.rdbuf(); allOut.clear(); if (mutexFlag) pthread_mutex_unlock(&mutexVal); chunkOut.clear(); chunkOut.seekp(0,ios::beg); //set put pointer at the beginning };
void File::insertion(char line[]) { int address = hash(line), counter = 0; char name[recordLen+1]; bool done = false, inserted = false; outfile.clear(); outfile.seekg(address,ios::beg); while (!done && outfile.get(name,recordLen+1)) { if (name[0] == empty || name[0] == delMarker) { outfile.clear(); outfile.seekg(address+counter*recordLen,ios::beg); outfile << line << setw(strlen(line)-recordLen); done = inserted = true; } else if (!strcmp(name,line)) { cout << line << " is already in the file\n"; return; } else counter++; if (counter == bucketSize) done = true; else outfile.seekg(address+counter*recordLen,ios::beg); } if (!inserted) { done = false; counter = 0; overflow.clear(); overflow.seekg(0,ios::beg); while (!done && overflow.get(name,recordLen+1)) { if (name[0] == delMarker) done = true; else if (!strcmp(name,line)) { cout << line << " is already in the file\n"; return; } else counter++; } overflow.clear(); if (done) overflow.seekg(counter*recordLen,ios::beg); else overflow.seekg(0,ios::end); overflow << line << setw(strlen(line)-recordLen); } }
void File::sortFile() { char rec[recordLen+1]; QSort(1,pointers[0]); // pointers[0] contains the # of elements; // put data from outfile in sorted order in file sorted: for (int i = 1; i <= pointers[0]; i++) { outfile.clear(); outfile.seekg(pointers[i]*recordLen,ios::beg); outfile.get(rec,recordLen+1); sorted << rec << setw(strlen(rec)-recordLen); } }
void File::operator- () { char c; // f2 will be this fp.open(fn,ios::in); int temp = fp.rdstate(); cout <<endl<< "after opening in read mode ; " <<temp; if(temp != 0) { fp.clear(); } fp.close(); fp.open (fn, ios::in | ios::out); int t = fp.rdstate (); cout << "\n In oprt - t=" << t; if (t == 0) { fp.seekg (0, ios::beg); while (1) { fp.get (c); cout <<endl<< "c = " << c; if (fp.eof ()) break; if (!fp.eof ()) { if (c >= 'A' && c <= 'Z') { /* AbCFdEGGD */ c += 32; fp.seekg (-1, ios::cur); fp.put (c); int temp = fp.rdstate(); cout << endl<<"after putting '" << c << "' into file : "<<temp; getchar(); } else if (c >= 'a' && c <= 'z') { c -= 32; fp.seekg (-1, ios::cur); fp.put (c); int temp = fp.rdstate(); cout << endl<<"after putting '" << c << "' into file : "<<temp; getchar(); } } } fp.close (); } }
void sft(int off, fstream& primary){ PrimaryIndex p, r; primary.seekg(off); primary.seekp(off); primary.clear(); primary.read(reinterpret_cast<char*>(&r), sizeof(PrimaryIndex)); while(!primary.eof() && r.Byte + 1){ off += sizeof(PrimaryIndex); p = r; primary.seekg(off); primary.read(reinterpret_cast<char*>(&r), sizeof(PrimaryIndex)); primary.seekp(off); if(primary.eof()){ primary.clear(); primary.write(reinterpret_cast<char*>(&p), sizeof(PrimaryIndex)); break; } primary.write(reinterpret_cast<char*>(&p), sizeof(PrimaryIndex)); } primary.clear(); }
int fnd(int x, fstream& primary, bool b){ primary.seekg(0, primary.end); primary.clear(); int l = 0, h = (primary.tellg()) / sizeof(PrimaryIndex) - 1, cr; PrimaryIndex p; while(l <= h){ cr = (l + h) / 2; primary.seekg(cr * sizeof(PrimaryIndex)); primary.read(reinterpret_cast<char*>(&p), sizeof(PrimaryIndex)); if(p.AccountNumber < x)l = cr + 1; else if(p.AccountNumber > x)h = cr - 1; else if(b){ primary.clear(); int x = p.Byte; p.Byte = -1; primary.seekp(cr * sizeof(PrimaryIndex)); primary.write(reinterpret_cast<char*>(&p), sizeof(PrimaryIndex)); return x; }else return (p.Byte == -1? -(cr + 1) * sizeof(PrimaryIndex): p.Byte); } return -(h + 2) * sizeof(PrimaryIndex); }
void File::excise(char line[]) { getName(line); int address = hash(line), counter = 0; bool done = false, removed = false; char name2[recordLen+1]; outfile.clear(); outfile.seekg(address,ios::beg); while (!done && outfile.get(name2,recordLen+1)) { if (!strcmp(line,name2)) { outfile.clear(); outfile.seekg(address+counter*recordLen,ios::beg); outfile.put(delMarker); done = removed = true; } else counter++; if (counter == bucketSize) done = true; else outfile.seekg(address+counter*recordLen,ios::beg); } if (!removed) { done = false; counter = 0; overflow.clear(); overflow.seekg(0,ios::beg); while (!done && overflow.get(name2,recordLen+1)) { if (!strcmp(line,name2)) { overflow.clear(); overflow.seekg(counter*recordLen,ios::beg); overflow.put(delMarker); done = removed = true; } else counter++; overflow.seekg(counter*recordLen,ios::beg); } } if (!removed) cout << line << " is not in database\n"; }
void File::combineFiles() { int counter = bucketSize*tableSize; char rec[recordLen+1]; outfile.clear(); overflow.clear(); outfile.seekg(0,ios::end); overflow.seekg(0,ios::beg); while (overflow.get(rec,recordLen+1)) { // transfer from if (rec[0] != delMarker) { // overflow to outfile only counter++; // valid (non-removed) items; outfile << rec << setw(strlen(rec)-recordLen); } } pointers = new long[counter+1]; // load to array pointers positions int arrCnt = 1; // of valid data stored in output file; for (int i = 0; i < counter; i++) { outfile.clear(); outfile.seekg(i*recordLen,ios::beg); outfile.get(rec,recordLen+1); if (rec[0] != empty && rec[0] != delMarker) pointers[arrCnt++] = i; } pointers[0] = --arrCnt; // store the number of data in position 0; }
//1ページ分をダンプ void DumpFile::Dump(){ cout << endl; m_file.clear(); m_file.seekg(m_page * PAGE_SIZE); for (int i = 0; i < PAGE_HEIGHT; ++i){ unsigned char buf[PAGE_WIDTH]; m_file.read((char*)buf,sizeof buf); for (int i = 0; i < m_file.gcount(); ++i){ printf("%02X ", buf[i]); } cout << endl; } }
void extractFromFile(fstream & statStream){ statStream.clear(); char buffer[256]; queue<string> Q; while(!statStream.eof()){ statStream.getline(buffer,256,'}'); Q.push(string(buffer)); } cout<<"size: "<<Q.size()<<endl; while(Q.size() !=0){ string first = Q.front(); Q.pop(); string second = Q.front(); Q.pop(); cout<<first<<" -- "<<second<<endl; filesDiff.push_back(make_pair(first,second)); } }
// ================================================================================================== bool c4s::search_file(fstream &target, const string &needle) /*! Uses Boyer-Moore algorithm to search for a text in a given stream. Stream needs to be opened before this function is called. Search begins from the current position. If match is found the file pointer is positioned to the start of the next needle. On error an exception is thrown. \param target Opened file stream to search for. \param needle String that should be found \retval bool True if needle was found, false if not. */ { const SIZE_T BMAX = 0x1000; char buffer[BMAX]; streamsize tg; SIZE_T br, boffset, total_offset, overlap=0; SIZE_T nsize = needle.size(); if(!target.good()) throw c4s_exception("search_file: given stream does not have 'good' status."); if(nsize >= BMAX) throw c4s_exception("search_file: size of search text exceeds internal read buffer size."); tg = target.tellg(); if(tg<0) throw c4s_exception("search_file: unable to get file position information."); total_offset = SIZE_T(tg); do { target.read(buffer+overlap,BMAX-overlap); br = SIZE_T(target.gcount()); if(search_bmh((unsigned char*)buffer, br+overlap, (unsigned char*)needle.c_str(), nsize, &boffset)) { target.clear(); target.seekg(total_offset+boffset,ios_base::beg); return true; } total_offset += br; memcpy(buffer, buffer+BMAX-nsize, nsize); if(!overlap) { overlap = nsize; total_offset -= nsize; } }while(!target.eof()); return false; }
//option 4 void DisplayAllMembers ( fstream& ioFile, bool& successful ) { ioFile.clear(); int lineNum = 0; if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { //start at the beginning of file ioFile.seekg(0); while(!ioFile.eof()) { //read the strucs ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //print the club members cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; } lineNum++; //if more than 8 members, pause if(lineNum == 8) { Pause(); lineNum = 0; } } successful = true; } }
void solve(int max_t,fstream&f){ /*read the ciphertext file into a char array*/ streampos begin,end; begin = f.tellg(); f.seekg(0,ios::end); end = f.tellg(); int textlen = end-begin; char* ciphertext = new char[textlen]; f.clear(); f.seekg(0,ios::beg); f.read(ciphertext,textlen); /*begin solve*/ KasiskiMethod(textlen,ciphertext); avgindexcoin(max_t,textlen, ciphertext); AutocorrelationMethod(max_t,textlen, ciphertext); delete[] ciphertext; }
// ====== FUNCTION DEFINITION ====== // Display any record in the file void dispRec(Inventory &product, fstream &fileName, int records) { fileName.clear(); // clear flags cout << "\n\tDisplay a record\n"; int recNum; // seek the file to the beginning fileName.seekg(0L, ios::beg); cout << "You have " << records << " records" << endl; cout << "Which record would you like to display? "; cin >> recNum; cin.ignore(); // ignore the newline char --recNum; // minus 1 to seek properly // seek to the record, store(read it), and display it fileName.seekg(sizeof(product) * recNum, ios::beg); fileName.read(reinterpret_cast<char *>(&product), sizeof(product)); cout << "Displaying record #" << (recNum + 1) << endl << endl; cout << "Item description: " << product.descr << endl; cout << "Quantity on hand: " << product.quantity << endl; cout << "Wholesale cost: " << product.wholesaleC << endl; cout << "Retail cost: " << product.retailC << endl; cout << "Date added to inventory (MM/DD/YYYY): " << product.dateAdded << endl; // seek the file back to the beginning fileName.seekg(0L, ios::beg); }
//option 5 void DisplayOneOrMoreMembers ( fstream& ioFile, bool& successful ) { ioFile.clear(); if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { string searchedName; ReadThisLine("Enter full or partial name of member(s) whose data " "you wish to display:",searchedName); //start at the beginning of file ioFile.seekg(0); while(!ioFile.eof()) { ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { //if found the searchedName(full or partial) if(((string)member.name).find(searchedName) != string::npos) { //print it cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; } } } successful = true; } }
void File::display () { char c; fp.clear(); fp.open (fn, ios::in); int t = fp.rdstate (); if (t != 0) { cout << "\n\t\tFile is not created yet, please create"; } else { fp.seekg (0, ios::beg); while (!fp.eof ()) { fp.get (c); if (!fp.eof ()) cout << c; } } fp.close (); }
//(PLIK -> TABLICA) Definicja funkcji wczytującej informacje o studentach z pliku "studenci.dat" do tablicy; tuple<Student *, int> wczytajPlikDoTablicy(string FILE_NAME){ //Otworzenie pliku "studenci.dat" w trybie "wejście/odczyt" studenci.open(FILE_NAME, ios::in); //Sprawdzenie czy plik został otwarty poprawnie if (studenci.good() == true) { int liczbaStruktur = 0; string linia; //Pętla zliczająca liczbę linii - rekordów while (getline(studenci, linia)) { liczbaStruktur++; } //Powrót do początku pliku tekstowego studenci.clear(); studenci.seekg(0, ios::beg); //Utworzenie tablicy o zadanej liczbie struktur typu student Student *tablica = new Student[liczbaStruktur]; //Pętla zapisująca dane z pliku do struktur; int i = 0; while (!studenci.eof()) { studenci >> tablica[i].imie >> tablica[i].nazwisko >> tablica[i].rokStudiow; i++; } //Zamknięcie pliku "studenci.dat" otwartego w trybie "wejście/odczyt" studenci.close(); //Funkcja zwraca dwa wyniki: tablicę z danymi oraz jej wielkość; return make_tuple((Student *)tablica, liczbaStruktur); }