Created
June 19, 2014 23:15
-
-
Save rolandsusans/bcfffa33bb39d3fd8f29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
struct binary | |
{ | |
void generate_write_bin_fail(fstream &fout, char *name,int amount) | |
{ | |
fout.close(); | |
fout.open(name, ios::out | ios::app); | |
for(int i=0; i<amount; i++) | |
{ | |
fout.write((char*)&i, sizeof(i)); | |
} | |
cout<<"Success, file :"<<name<<" has been created!"<<endl; | |
fout.close(); | |
}; | |
int read_amount_of_data(fstream &fin, char *name) | |
{ | |
fin.close(); | |
fin.open(name, ios::in); | |
fin.seekg(0,ios::beg); | |
int counter=0; | |
int data; | |
while (fin) | |
{ | |
fin.read((char*)&data, sizeof(data)); | |
if(fin)counter++; | |
} | |
fin.close(); | |
return counter; | |
}; | |
void delete_last_two(fstream &file, char *name, int counter) | |
{ | |
char name2[]="system_data.bin"; | |
fstream fout(name2, ios::out | ios::app); | |
file.close(); | |
file.open(name, ios::in); | |
int data; | |
if(counter<=2) | |
{ | |
file.close(); | |
fout.close(); | |
remove(name);//deltes old file | |
rename(name2,name);//replace with empty | |
} | |
else{ | |
for(int i=0; i<(counter-2); i++) | |
{ | |
file.read((char*)&data, sizeof(data));//reads data from file | |
fout.seekg(0, ios::end); | |
fout.write((char*)&data, sizeof(data));//writes to system_file | |
} | |
file.close(); | |
fout.close(); | |
remove(name);//deltes old file | |
rename(name2,name); | |
} | |
}; | |
void print_on_screen_content(fstream &file, char *name) | |
{ | |
int data; | |
file.close(); | |
file.open(name,ios::in); | |
cout<<"Faila saturs:"<<endl; | |
while(file) | |
{ | |
file.read((char*)&data, sizeof(data)); | |
if(file){cout<<data;}; | |
}; | |
cout<<endl; | |
file.close(); | |
} | |
}; | |
int main() | |
{ | |
char name[]="yolo.bin"; | |
binary bin;//crate struct object | |
fstream file(name, ios::out);//create empty file | |
cout<<"Cik vertibas savadit binara faila?"<<endl; | |
int amount; | |
cin>>amount; | |
bin.generate_write_bin_fail(file, name, amount); | |
bin.print_on_screen_content(file,name); | |
int size; | |
size = bin.read_amount_of_data(file,name); | |
bin.delete_last_two(file,name,size); | |
bin.print_on_screen_content(file,name); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment