Code:
#ifndef ELEM_H
#define ELEM_H
#include <iostream>
#include <fstream>
using namespace std;
const int size=30;
class elem
{
public:
elem();
void setkey(int);
int getkey();
void setnext(int);
int getnext();
void setprev(int);
int getprev();
void setinfo(char *);
void add(fstream &);
private:
int key;
int next;
int prev;
char info[size];
};
elem::elem()
{
key=next=prev=0;
for(int i=0;i<30;i++)
{
info[i]=' ';
}
}
void elem::setkey(int k)
{
key=k;
}
int elem::getkey()
{
return key;
}
void elem::setnext(int nex)
{
next=nex;
}
int elem::getnext()
{
return next;
}
void elem::setprev(int pre)
{
prev=pre;
}
int elem::getprev()
{
return prev;
}
void elem::setinfo(char * inf)
{
for (int i=0;i<30;i++)
{
info[i]=inf[i];
}
}
void elem::add(fstream &fout)
{
fout.seekp(0,ios::end);
fout.write((char*)&key,sizeof(int));
fout.write(info,size);
fout.write((char*)&prev,sizeof(int));
fout.write((char*)&next,sizeof(int));
fout.clear();
}
#endif
this is header
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include "elem.h"
#include <string>
using namespace std;
bool menu(int &); //menu lietotajam
int findPosNext(fstream &,int); //atrodam nakamo atslegu
int findPosPrev(fstream &,int); //atrodam iepriekseo atslegu
void initPoint(fstream &); //uzstadam nakamo uz ieprieksejo atslegu
void print(fstream &); //izdruka
void printBack(fstream &); //izdruka
int main()
{
int ok=1,num=0,choice=1;
char inff[30];
string filen;
do{
cout<<"Enter the name of the file with an extension (.bin)\n";
cin>>filen;
fstream f;
f.open(filen.c_str(),ios::out|ios::in|ios::binary);
//lietotais ievada faila nosaukumu
while(menu(choice)) //izveidojam pec tam atveram failu
{ //kamer lietotais neievada -100
switch(choice)
{
case 1: //izveido objektu
{elem obj1; //ieraksta atslegu un saturu
cout<<"\nEnter the Key of record\n";
cin>>num;
obj1.setkey(num);
cout<<"Enter the value of record\n";
cin.getline(inff,30,'/0');
obj1.setinfo(inff);
obj1.add(f); //pievieno componentu failam
initPoint(f);} //uzstada next un prev
break;
case 2:
{print(f);} //izdruka
break;
case 3:
{printBack(f);} //izdruka
break;
}
}
f.close();
cout<<"Procceed with new file? (1) or exit (0) \n"; //atkartosanas iespeja
cin>>ok;
}
while(ok!=0);
return 0;
}
bool menu(int &choice) //lietotaja izvele
{
cout<<"\nEnter your choice\n"
<<"1 - to add a record to file\n"
<<"2 - to print\n"
<<"3 - to print backwards\n"
<<"-100 to end this file proccessing\n";
cin>>choice;
return choice!=-100;
}
int findPosNext(fstream &f,int key) //ejam caur failu
{ //meklejam vismazako starpibu starp divam key
int next=0,cur,gap=32000; //un lai key butu mazak
f.seekg(0,ios::beg);
while(f)
{
f.read((char*)&cur,sizeof(int));
if(fabs((double)cur-(double)key)<gap&&key<cur)
{
next=cur;
gap=fabs((double)cur-(double)key);
}
f.seekg(38,ios::cur);
}
f.clear();
return next; //atgriezam next atslegu
}
int findPosPrev(fstream &f,int key) //tas pats bet meklejam mazako starpibu
{ //un lai key butu mazak
int prev=0,cur,gap=32000;
f.seekg(0,ios::beg);
while(f)
{
f.read((char*)&cur,sizeof(int));
if(fabs((double)cur-(double)key)<gap&&key>cur)
{
prev=cur;
gap=fabs((double)cur-(double)key);
}
f.seekg(38,ios::cur);
}
f.clear();
return prev;
}
void initPoint(fstream &f) //ejam caur failu
{ //atrodam katram key , nakamo atslegu
int key=0,prev=0,next=0,pos=0; //uzstadam to, katrai komponentei
f.seekg(0,ios::beg);
f.read((char*)&key,sizeof(int));
while(f)
{
pos=f.tellg();
prev=findPosPrev(f,key);
next=findPosNext(f,key);
f.seekp(pos+30,ios::beg);
f.write((char*)&prev,sizeof(int));
f.write((char*)&next,sizeof(int));
f.seekg(pos+38,ios::beg);
f.read((char*)&key,sizeof(int));
}
f.clear();
}
void print(fstream &f) //ejam caur failu , atrodam mazako key
{ //drukam to , skatamies uz next
int key=0,next=1,pos=0,small=32000; //ejam caur failu , atrodam next , drukam to
char inff[size];
f.seekg(0,ios::beg);
while(f)
{
f.read((char*)&key,sizeof(int));
if(key<small)
{
small=key;
pos=f.tellg();
}
f.seekg(38,ios::cur);
}
f.clear();
key=small;
while(next!=0)
{
f.seekg(pos,ios::beg);
cout<<key<<" ";
f.read(inff,size);
cout<<inff<<endl;
f.seekg(4,ios::cur);
f.read((char*)&next,sizeof(int));
if(next!=0)
{
f.seekg(0,ios::beg);
while(f)
{
f.read((char*)&key,sizeof(int));
if(key==next)
{
pos=f.tellg();
break;
}
f.seekg(38,ios::cur);
}
f.clear();
}
}
f.clear();
}
void printBack(fstream &f) //viss tas pats bet sakuma atrodam lielako key
{ //un pec meklejam prev
int key=0,next=1,pos=0,small=0;
char inff[size];
f.seekg(0,ios::beg);
while(f)
{
f.read((char*)&key,sizeof(int));
if(key>small)
{
small=key;
pos=f.tellg();
}
f.seekg(38,ios::cur);
}
f.clear();
while(next!=0)
{
f.seekg(pos,ios::beg);
cout<<key<<" ";
f.read(inff,size);
cout<<inff<<endl;
f.read((char*)&next,sizeof(int));
if(next!=0)
{
f.seekg(0,ios::beg);
while(f)
{
f.read((char*)&key,sizeof(int));
if(key==next)
{
pos=f.tellg();
break;
}
f.seekg(38,ios::cur);
}
f.clear();
}
}
f.clear();
}
so basically , the problem is that, when i am reading input from user ( with cin>> , or cin.getline) the program takes the input and fills c-style string info with it( for example i input "asdasdasd", and program fills string with this information) , then it adds terminating symbol (/0) , and then , if there is some space unused, the program fills this space with ASCII representation of the byte 0xCC(found this at some website) , anyway , i don't know , how to get rid of it