Skip to content

Instantly share code, notes, and snippets.

@rolandsusans
Created June 18, 2014 08:22
Show Gist options
  • Save rolandsusans/632e1a43caefbcff37b5 to your computer and use it in GitHub Desktop.
Save rolandsusans/632e1a43caefbcff37b5 to your computer and use it in GitHub Desktop.
/***
=== H13 ===
Izveidot programmu valodā C++. Ja programma darbojas ar failu, nedrīkst dublēt visa faila saturu operatīvajā atmiņā.
Ar faila komponenti tiek saprasts fiksēta garuma ieraksts. Sīkākās prasības sk. Laboratorijas darbu noteikumos.
H13. Uzrakstīt programmu, kas ļauj izveidot un labot bināru failu,
kura glabājas datumi, un kura ieraksta struktūra ir sekojoša:
gads (int), mēnesis (int), diena (int), ieraksta statuss (0 vai 1).
Programmai katrs jauns ieraksts jāieliek faila beigās. Ja failā tāds datums jau eksistē, tas nav jāpieliek.
Jāparedz iespēja:
(1) izmest faila komponenti (loģiski atzīmējot kā izmestu),
(2) izdrukāt failā esošos datumus uz ekrāna,
(3) izmest loģiski izmestas komponentes fiziski.
Rolands Usāns, RU10008, 2014.06.17
***/
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
struct binary
{
void write(fstream &file,char *name,int year, int month, int day, int status_good)//checks if there is value in file, writes value to file
{
file.close();
file.open(name, ios::in);
int ch_year;
int ch_month;
int ch_day;
int ch_status=0;
file.seekg(0,ios::beg);
file.read((char*)&ch_year, sizeof(ch_year));//read test value
if(file)//checks if file is empty
{
file.close();
file.open(name,ios::in);
file.seekg(0,ios::beg);
while(file)
{
file.read((char*)&ch_year, sizeof(ch_year));//reads year
file.read((char*)&ch_month, sizeof(ch_month));//reads month
file.read((char*)&ch_day, sizeof(ch_day));//reads day
file.read((char*)&ch_status, sizeof(ch_status));//reads logical status
if(file){//check if end of file is reached
if(ch_year==year and ch_month==month and ch_day==day and ch_status==status_good)
{
cout<<endl<<"ieraksts jau eksiste"<<endl;
file.close();
break;
return;
}
}else//writes element to end of file, if element not found
{
file.close();
file.open(name, ios::out | ios::app);
file.seekg(0, ios::end);
file.write((char*)&year, sizeof(year));
file.write((char*)&month, sizeof(month));
file.write((char*)&day, sizeof(day));
file.write((char*)&status_good, sizeof(status_good));
file.close();
break;
return;
}
}
}else//writes first date to file
{
file.close();
file.open(name, ios::out);
file.seekg(0,ios::beg);
file.write((char*)&year, sizeof(year));
file.write((char*)&month, sizeof(month));
file.write((char*)&day, sizeof(day));
file.write((char*)&status_good, sizeof(status_good));
file.close();
}
};
void print(fstream &fin, char *name)//print file content
{
fin.open(name,ios::in);
fin.seekg(0, ios::beg);
int info;
cout<<"Binara faila saturs"<<endl;
cout<<setw(4)<<"yyyy"<<setw(4)<<"mm"<<setw(4)<<"dd"<<setw(8)<<"status"<<endl;
while(fin)
{
for(int i=0;i<4;i++)
{
fin.read((char*)&info, sizeof(info));
if(fin){cout<<setw(4)<<info;}
}
if(fin)cout<<endl;
};
fin.close();
};
void logical_delete(fstream &file, char *name)//deletes from file all fields with logical status 0
{
int read_year;
int read_month;
int read_day;
int read_status;
char name2[]="data2.txt";
fstream logical(name2, ios::out);
file.close();
file.open(name,ios::in);
file.read((char*)&read_year, sizeof(read_year));//reads test value
if(file){
file.seekg(0, ios::beg);
while(file){
file.read((char*)&read_year, sizeof(read_year));//reads year
file.read((char*)&read_month, sizeof(read_month));//reads month
file.read((char*)&read_day, sizeof(read_day));//reads day
file.read((char*)&read_status, sizeof(read_status));//reads logical status
if(file){//check if end of file is reached
if(read_status)
{
write(logical,name2,read_year,read_month,read_day, 1);
};
}else
{
file.close();
logical.close();
remove(name);//deltes old file
rename(name2,name);
}
}
}else
{
cout<<"ERROR:Faila neka nav!";
file.close();
logical.close();
remove(name);//deltes old file
rename(name2,name);
}
};
void set_logical_delete(fstream & file, char *name, int log_year, int log_month, int log_day)
{
int read_year;
int read_month;
int read_day;
int read_status;
char name2[]="data2.txt";
fstream logical(name2, ios::out);
file.close();
file.open(name,ios::in);
file.read((char*)&read_year, sizeof(read_year));//reads test value
if(file){
file.seekg(0, ios::beg);
while(file)
{
file.read((char*)&read_year, sizeof(read_year));//reads year
file.read((char*)&read_month, sizeof(read_month));//reads month
file.read((char*)&read_day, sizeof(read_day));//reads day
file.read((char*)&read_status, sizeof(read_status));//reads logical status
if(file){//check if end of file is reached
if(read_year==log_year && read_month==log_month && read_day==log_day)
{
write(logical,name2,read_year,read_month,read_day, 0);
}
else
{
if(read_status==1)
write(logical,name2,read_year,read_month,read_day, 1);
else write(logical,name2,read_year,read_month,read_day, 0);
}
}
else
{
file.close();
logical.close();
remove(name);//deltes old file
rename(name2,name);
}
}
}
else
{
cout<<"ERROR:Faila neka nav!"<<endl;
file.close();
logical.close();
remove(name);//deltes old file
rename(name2,name);
}
}
void print_menu()//prints menu
{
cout<<"======================================================================="<<endl;
cout<<"1. Izdrukat sarakstu."<<endl;
cout<<"2. Pievienot saraksta elementus."<<endl; //visas iespejamas darbibas
cout<<"3. Veikt logisko dzessanu."<<endl;
cout<<"4. Veikt fizisko dzessanu"<<endl;
cout<<"0. Ievadiet 0, lai beigtu"<<endl;
cout<<"======================================================================="<<endl;
cout<<"Ievadiet darbibas numuru lai to izpilditu: ";
}
};
int main ()
{
char name[]="data.bin";//file name
int year;
int month;
int day;
int status=1;//default
int amount;
cout<<"Cik datums velaties ievadit?"<<endl;
cin>>amount;
binary bin;//creats structure
fstream file(name, ios::out);
for(int i=0;i<amount; i++)
{
cout<<"Ievadiet datumu formata: yyyy mm dd"<<endl;
cin>>year;
cin>>month;
cin>>day;
bin.write(file,name,year,month,day, 1);//writes data to file
}
bin.print(file, name);
int choise=1;
while(choise)
{
bin.print_menu();
cin>>choise;
if(choise==0){return 0;}
else if(choise==1)//prints out file contents
{
bin.print(file, name);
}
else if(choise==2) //adds new date, checks..,
{
cout<<"Ievadiet datumu formata: yyyy mm dd"<<endl;
cin>>year;
cin>>month;
cin>>day;
bin.write(file,name,year,month,day, 1);//writes data to file
}
else if(choise==3)//sets logical delete
{
cout<<"Ievadiet datumu formata: yyyy mm dd, kuru logiski dzest"<<endl;
cin>>year;
cin>>month;
cin>>day;
bin.set_logical_delete(file,name,year,month,day);
}
else if(choise==4)//deletes logical 0 field values from file
{
bin.logical_delete(file, name);
cout<<"DONE!"<<endl;
}
else//returns 0
{
return 0;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment