void write_comb_c(fstream &f) { cout<<"Combinations are being processed.........\nPlease Wait........"; char cb[6][3][3],cb_n[6][3][3],cb_o[6][3][3]; int max_mv=0; long int s=0,cur_rec; cb_comb c,c_n,c_o; f.open("combinat.dat",ios::in|ios::binary|ios::nocreate); f.seekg(-sizeof(c),ios::end); f.read((char*)(&c),sizeof(c)); f.close(); max_mv=c.r_moves()-1; f.open("combinat.dat",ios::in|ios::binary|ios::nocreate); f.seekg(0); while(f.read((char*)(&c),sizeof(c))) if(max_mv==c.r_moves()) { cur_rec=(f.tellg()/sizeof(c))-1; break; } f.close(); cout<<cur_rec; getch(); c.cpy_cube(cb); while(max_mv==c.r_moves()) { for(int i=0;i<6;i++) for(int j=0;j<2;j++) { c_n.new_comb(cb,v[i],dr[j],max_mv); c_n.cpy_cube(cb_n); s=0; f.open("combinat.dat",ios::in|ios::binary|ios::nocreate); f.seekg(0); while(f.read((char*)(&c_o),sizeof(c))) { c_o.cpy_cube(cb_o); if(c_o.r_moves()<=c_n.r_moves()) s+=cmp_cb(cb_n,cb_o); if(s>0) break; } f.close(); if(s==0) { f.open("combinat.dat",ios::app|ios::binary|ios::nocreate); f.write((char*)(&c_n),sizeof(c)); f.close(); } } cur_rec++; f.open("combinat.dat",ios::in|ios::binary|ios::nocreate); f.seekg(sizeof(c)*cur_rec); f.read((char*)(&c),sizeof(c)); c.cpy_cube(cb); f.close(); } return; }
static T peek_value_from_stream(fstream& fs) { const std::streampos original_pos = fs.tellg(); T data; fs.read(reinterpret_cast<char*>(&data), sizeof data); fs.seekg(original_pos); return data; }
/*size of file*/ streamoff file_size(fstream &myfile) { streamoff size = 0; myfile.seekg(0, ios::end); size = myfile.tellg(); myfile.seekg(0, ios::beg); return size; }
void Decryptor::read_Input(char* inFileName) { ifstream inFile(inFileName, ios::binary); inFile.seekg(0, ios::end); //Select Text form beginning to end flow.reserve(inFile.tellg()); //Reserve The lenght of the text for said string inFile.seekg(0, ios::beg); //Return the pointer to the start flow.assign((istreambuf_iterator<char>(inFile)),istreambuf_iterator<char>()); }
int count() { file.seekg(0, ios::end);//novieto pozīciju beigās int n = (int)(file.tellg() / sizeof(record)); //pajautaa, kur atrodas seekg poziicija file.seekg(0); return n; }
bool find(fstream& file,unsigned int& loc, Employee& F){ Employee E; bool found = false; while(!file.fail()){ file.read((char*)&E, sizeof(Employee)); if(E == F){ loc = (unsigned int)file.tellg() - sizeof(E); found = true; } } return found; }
void Encryptor::read_Input(char* inFileName) { ifstream inFile(inFileName, ios::binary); string flow1; inFile.seekg(0, ios::end); flow1.reserve(inFile.tellg()); inFile.seekg(0, ios::beg); flow1.assign((istreambuf_iterator<char>(inFile)),istreambuf_iterator<char>()); //There is a difference between flow and flow1 for debuggin purpose to be able to compare integrity flow = flow1; }
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; }
CXXFStream(const char* _name,ios_base::openmode _mode):file(_name,_mode|=ios::binary) { if(!file) { size = 0; return; } file.seekg(0, ios::end); size = (long)file.tellg(); file.seekg(0, ios::beg);//将指针重新定位到文件开头 int i; for(i=0;i<10;++i) //0~9 ox2d[i] = i; for(i=49;i<55;++i) //a~f、A~F ox2d[i] = ox2d[i-32] = i-39; }
void student::del(char *key) { int x=search(key); if(x==-1) return; char buf[100],buf1[100],buf2[100],bufx[100]; fd.close(); fd.open("x.txt",ios::in); fd.seekg(0,ios::beg); while(fd) { fd.getline(buf,100,'\n'); strcpy(buf1,buf); strtok(buf1,"|"); char *ptr=strtok(NULL,"\n"); strcpy(bufx,ptr); if(!strcmp(buf1,"\0")){cout<<"\n\nNo data error";getch();return;} if(strcmp(key,buf1)==0) { cout<<"\n\nKey found in del"<<buf; strcat(buf,"\n"); int y=strlen(buf); int tel=fd.tellg(); fd.close(); fd.open("x.txt",ios::out|ios::in); fd.seekp(tel,ios::beg); fd.seekp(-(y+1),ios::cur); fd<<"*"; fd.close(); int q=atoi(ptr); fd.open("file.txt",ios::out|ios::in); fd.seekp(q,ios::beg); fd<<"*"; fd.close(); cout<<"\n\nKey deleted"; return; } } fd.close(); cout<<"\n\nCouldnt del"; return; }
void find_rrn() { int pos; string seg; fp1.open("record4.txt",ios::in); if(!fp1) error(1); while(fp1) { pos = fp1.tellg(); getline(fp1,seg); if(seg.length()==0) continue; rrn[++cnt] = pos; } fp1.close(); }
void student::pack() { read(); char buf[100]; fd.close(); fd.open("file.txt",ios::app); strcpy(buf,usn); strcat(buf,"|"); strcat(buf,name); strcat(buf,"|"); strcat(buf,sem); strcat(buf,"|"); strcat(buf,dept); strcat(buf,"#"); fd<<buf; fd.close(); fd1.close(); /* Primary Index */ fd1.open("x.txt",ios::app); strcpy(buf,usn); strcat(buf,"|"); fd1<<buf; fd1.close(); fd1.open("x.txt",ios::app); fd1<<pos; fd1.close(); fd1.open("x.txt",ios::app); fd1<<endl; fd1.close(); fd.open("file.txt",ios::in); fd.seekg(0,ios::end); pos=fd.tellg(); fd.close(); fd3.open("c.txt",ios::app); /* Secondary Index */ strcpy(buf,name); strcat(buf,"|"); strcat(buf,usn); strcat(buf,"\n"); fd3<<buf; fd3.close(); }
/** gets the last id entered if it's an existing file ** returns 0 otherwise. */ int getId(fstream &input) { int i=0; //initialized the number of id to 0 int j; input.seekp(0,ios::end); //sets the pointer to end of the stream file if (input.tellg() != 0) //if the pointer is not 0 we know that the file is not empty { char awesome[3]; input.seekg(-2,ios::end); input.get(awesome,3); i = atoi(awesome); input.seekg(-2,ios::cur); input.getline(awesome,3); j = atoi(awesome); if (j) return j; return i; } else return i; //else the file is empty and id is 0 }
// ================================================================================================== 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; }
void deflate(fstream &in, fstream &out) { InWindow slWindow(in); OutBuffer buf(out); in.seekg(0, in.end); int size = in.tellg(); in.seekg(0, in.beg); do { short BFINAL; if (size > MAX_BLOCK_SIZE) BFINAL = 0; else BFINAL = 1; buf.writebits(BFINAL, 1, true); int blockSize = min(size, MAX_BLOCK_SIZE); deflate_tree(slWindow, buf, blockSize); size -= blockSize; } while (size); buf.~OutBuffer(); }
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 fileCopy(fstream &read, fstream &write){ //Copys contents of "read" into "write" int fileSize = 0; int position = 0; int chunkSize = 1024; char* chunkBuffer = new char[chunkSize]; //Write the asset data read.seekg(0,ios::end); fileSize = (int)read.tellg(); read.seekg(0, ios::beg); write.write(reinterpret_cast<char*>(&fileSize),sizeof(int)); while (position<fileSize) { int readSize = chunkSize; if (position+chunkSize>fileSize) { readSize = fileSize-position; } //Copy the data over read.read(chunkBuffer,readSize); write.write(chunkBuffer,readSize); position+=readSize; } delete [] chunkBuffer; }
void student::del1(char *key) { char buf[100],buf1[100],buf2[100],bufx[100]; fd.close(); fd.open("c.txt",ios::in); fd.seekg(0,ios::beg); while(fd) { fd.getline(buf,100,'\n'); strcpy(buf1,buf); strtok(buf1,"|"); char *ptr=strtok(NULL,"\n"); strcpy(bufx,ptr); if(!strcmp(buf1,"\0")){cout<<"\n\nNo data error";getch();return;} if(strcmp(key,buf1)==0) { strcat(buf,"\n"); int y=strlen(buf); int tel=fd.tellg(); fd.close(); fd.open("c.txt",ios::out|ios::in); fd.seekp(tel,ios::beg); fd.seekp(-(y+1),ios::cur); fd<<"*"; fd.close(); del(bufx); return; } } fd.close(); cout<<"\n\nCouldnt del"; return; }
void create_checksum_test_file( fstream& fs, const string& test_file_name ) { fs.seekg( 0, ios::end ); fstream::pos_type size = fs.tellg( ); fs.seekg( 0, ios::beg ); MD5 md5; unsigned char buffer[ c_buffer_size ]; int size_left = ( int )size; while( size_left ) { int next_len( min( size_left, c_buffer_size ) ); fs.read( ( char* )buffer, next_len ); int len = fs.gcount( ); md5.update( buffer, len ); size_left -= next_len; } md5.finalize( ); fs.seekg( 0, ios::beg ); stringstream ss; ss << md5.hex_digest( ); ofstream outf( test_file_name.c_str( ), ios::out | ios::binary ); if( !outf ) throw runtime_error( "Unable to open file '" + test_file_name + "' for output." ); outf << ss.str( ); outf.flush( ); if( !outf.good( ) ) throw runtime_error( "Unexpected error occurred whilst writing to file '" + test_file_name + "'." ); }
int MipsDataSet::LoadHWS(fstream& ffbin) { uint16_t version; uint8_t datatype; int res = 0; HWSReadShort(ffbin,version); HWSReadChar(ffbin,datatype); while (datatype!=0xFF) { if (datatype==MIPS_HWS_BATTLE_DATA) { uint32_t am; HWSReadLong(ffbin,am); if (am>battle_code.amount) { res |= 1; HWSSeek(ffbin,ffbin.tellg(),4*am); } else { battle_code.amount = am; battle_code.ReadHWS(ffbin); SetupBattleFunction(); battle_modified = true; } } HWSReadChar(ffbin,datatype); } return 0; }
void function2(){ ofstream out("131250072_2.txt"); fstream temp_file("temp.txt", ios::out | ios::in | ios::trunc);//must add ios::trunc to create the file if it is not exist if (back.is_open() && out.is_open() && temp_file.is_open()) { int count_of_word = 0; int diff_word = 0; back.seekg(0, ios::end); int size = back.tellg(); // cout << size << endl; char *s = new char[size] ;//save all file in it back.seekg(0, ios::beg);//start from beginning, for ios::end while (back.getline(s, size)){//TODO why I have to write a endl to make back readable??? cout << "one time" << endl; int beg_of_word = 0; int len = 0; while (split(&s[beg_of_word], &len)) {//find the first appearence of word count_of_word = 1; diff_word++; const int dul = len; const int duw = beg_of_word; char s1[MAX_WORD] = {0}; char s2[MAX_WORD] = {0}; strncpy(s1, empty_word, 100); strncpy(s1, &s[duw], dul); for (beg_of_word+=len ;split(&s[beg_of_word], &len); beg_of_word+=len) {//scan the rest part, so to count the times of it strncpy(s2, empty_word, 100); strncpy(s2, &s[beg_of_word], len); if (my_strcmp(s1, s2) == 0){//compare strings ignoring the space count_of_word ++;//count of same word memset((void*)&s[beg_of_word], ' ', len);//set the same word in array blank } } beg_of_word = duw;//find the beginning of last word beg_of_word += dul;//to next word //write to a file extract_word(s1); temp_file << count_of_word << endl << s1 << endl; } //make tokens Token *tokens = new Token[diff_word]; int in_token = 0; char tem_w[MAX_WORD] = {0}; bool num_word = true;//true means it is a num, false means it's a word temp_file.seekp(0, ios::beg);//TODO why I have to add it to make it readable??? write to it will change the get position? while(temp_file.getline(tem_w, MAX_WORD)){//getline will clear the last word's wreakage if (num_word) { tokens[in_token].times = atoi(tem_w); num_word = false; } else { strncpy(tokens[in_token++].str, tem_w, MAX_WORD); num_word = true; } } //sort sort_token(tokens, 0, diff_word); //print for (int i = 0; i < diff_word; ++i) { out << tokens[i].str << " " << tokens[i].times << endl; } delete[] tokens; } } else { cout << out.is_open() << endl; cout << temp_file.is_open() << endl; cout << back.is_open() << endl; cout << "can't open file" << endl; } }
int64_t Local_File::offset() { return file.tellg(); }
size_t size() { file.seekg(0, ios_base::end); return static_cast<size_t>(file.tellg()); }
//option 7 void UpdateMemberData ( fstream& ioFile, bool& successful ) { //store the position of the stream(later to be used for seekg/p) streamoff position; ioFile.clear(); int count = 0; if(!ioFile.is_open()) { cout << "\nBinary file not yet open.\nReturning to menu." << endl; Pause(); successful = false; } else { string identity; ReadThisLine("Enter enough of the member's name to " "identify him/her: ", identity); //start at beginning ioFile.seekg(0); while(!ioFile.eof()) { //make a temporary position to hold the beginning of the struc streamoff tempPos = ioFile.tellg(); //read the binary file ioFile.read((char *)&member, sizeof(ClubMember)); if(!ioFile.eof()) { if(((string)member.name).find(identity) != string::npos) { //put the beginning of the struc into the position position = tempPos; //print club member cout << "====================\n" << member.name << endl; cout << "Age: " << member.age << " " << "Balance: $" << member.balance << endl; count++; } } } if(count != 1) { cout << "\n=====>Either more than one member, or no members, " "matched your input.\n=====>You need to identify a " "unique club member to update.\n=====>Try again with " "more specific input.\n" << endl; Pause(); successful = false; } if(count == 1) { cout << "\nUpdating data for the above club member." << endl; string choice; do { cout << "Enter n/a/b to update name/age/balance, or " "q to quit updating this member: "; getline(cin,choice); if(choice == "q") { cout << "\nOK, no more updates for this club member " "at this time." << endl; Pause(); successful = true; } if(choice == "n") { string revisedName; ReadThisLine("Enter revised name: ", revisedName); //clear the array memset(member.name,'\0',31); for(unsigned int i = 0 ; i < revisedName.length(); i++) //store the new name into the struc member.name[i] = revisedName[i]; } if(choice == "a") { int newAge; ReadInt("Enter revised age: ", newAge); //store new age in struc member.age = newAge; } if(choice == "b") { double newBalance; ReadDouble("Enter revised balance: ", newBalance); //store new balance in struc member.balance = newBalance; } } while(choice != "q"); } ioFile.clear(); //go to the position ioFile.seekg(position); ioFile.seekp(position); //finally write the info back into the binary file ioFile.write((char *)&member, sizeof(ClubMember)); successful = true; } }
fstream::pos_type operator()() { return file.tellg(); }
void ScanFile(string path) { cout << path << endl; //system("pause"); //cout << path << endl; //cin.get(); string line_read; stringstream line_write_ss; string line_write; string Quote_author; bool duplicate; unsigned long tempfile_size; string line; string feld; unsigned int line_pos=0; unsigned short feld_pos=0; vector<int> Pos(7); int i; char infile_char; infile_ScanFile.open(path, ios::in|ios::binary); if (infile_ScanFile.is_open()) { unsigned int aaa=0; unsigned int bbb=0; /* while (infile_ScanFile.get(infile_char)) { aaa++; if(aaa==1) { aaa=0; bbb++; cout << infile_char; } } system("pause"); */ while (infile_ScanFile.get(infile_char)) { if(infile_char==Find[0][Pos[0]]) { Pos[0]++; } else { Pos[0]=0; } if(Pos[0]==Find_length[0]) break; } // //} while(1) { //cout << line << endl; //memset(line,0,sizeof(line)); feld_pos=0; line.clear(); feld.clear(); //infile_ScanFile.get(infile_char); while(infile_ScanFile.get(infile_char)) { //cout << aaa++ << endl; //continue; //cout << "#"; //cout << infile_char; line.push_back(infile_char); if(infile_char=='\n') break; //new_feld:; if(infile_char==' ') { infile_ScanFile.get(infile_char); line.push_back(infile_char); if(infile_char=='\'') { infile_ScanFile.get(infile_char); line.push_back(infile_char); if(infile_char=='\'') continue; //memset(hash_char,0,sizeof(hash_char)); if(feld_pos==1) { while(1) { //cout << infile_char; //cout << "#"; if(infile_char=='\'') { infile_char_finchar_1:; infile_ScanFile.get(infile_char); line.push_back(infile_char); if(infile_char==',') { //infile_ScanFile.get(infile_char); line.push_back(infile_char); break; } else if (infile_char=='\'') { goto infile_char_finchar_1; //for the rare but possible ''', case! } else { //break; feld.push_back('\''); i++; feld.push_back(infile_char); } } feld.push_back(infile_char); infile_ScanFile.get(infile_char); line.push_back(infile_char); } } //cout << line << endl; feld_pos++; /* if(feld_pos==2) { aaa++; if(aaa==10000) { aaa=0; bbb++; for(i=0;i<40;i++) { cout << feld[i]; } cout << endl; //system("pause"); } } */ } else { if(infile_char=='\n') break; } } else { if(infile_char=='\n') break; } } //cout << endl;// << endl << endl; //system("pause"); //cout << line << endl; /* for(i=0;i<2000;i++) { cout << line[i]; } system("pause"); */ /* aaa++; if(aaa==10000) { aaa=0; bbb++; for(i=0;i<40;i++) { cout << feld[i]; } cout << endl; //system("pause"); } continue; */ cout << feld << endl; if(feld.length()<40) continue; for(c.at(0)=48;c.at(0)<58;c.at(0)++) for(c.at(0)=97;c.at(0)<123;c.at(0)++) outfile_ss << "./temp/" << feld[0] << "/" << feld[1] << feld[2] << feld[3]; //cout << feld << endl; tempfile.open(outfile_ss.str(), ios::in|ios::out|ios::binary|ios::app); duplicate=false; tempfile.seekg(0, tempfile.end); //cout << tempfile.tellg() << endl; tempfile_size=tempfile.tellg(); if(tempfile_size>0) { tempfile.seekg(0, tempfile.beg); while(tempfile_size>tempfile.tellg()+7) { getline(tempfile, line_read); //cout << line << endl; if(line==line_read) { duplicate=true; torrent_duplication_cont_temp++; //cout << "Duplicate!" << endl; break; } } } torrent_count_temp++; if(torrent_count_temp==1000) { torrent_count+=torrent_count_temp; torrent_count_temp=0; torrent_duplication_cont+=torrent_duplication_cont_temp; clock_duration = (clock()-torrent_clock) / (double) CLOCKS_PER_SEC; cout << torrent_count << " Torrents in " << clock()/(double)CLOCKS_PER_SEC << "s and " << clock_duration << "s/part avg=" << (clock()/(double)CLOCKS_PER_SEC/torrent_count*1000) << "s/l D/p=" << torrent_duplication_cont_temp << " Dubl=" << torrent_duplication_cont << "\nFile:" << path << '\n' << endl; torrent_duplication_cont_temp=0; torrent_clock = clock(); } if(duplicate==false) { //cout << line << endl; tempfile << line; } tempfile.close(); outfile_ss.str(""); outfile_ss.clear(); //memset(line,0,sizeof(line)); //system("pause"); } } infile_ScanFile.close(); }
bool commons::utils::streams::is_size_less_than(fstream &file, int byte_size) { file.seekg(0, std::ios::end); int position = file.tellg(); file.seekg(0); return byte_size >= position; }
void set_to_current_pos(fstream& fs) { this->m_absolute_position = fs.tellg(); }
std::ifstream::pos_type filesize(fstream &in) { in.seekg(0, std::ifstream::end); return in.tellg(); }