Friday, 9 August 2013

Errror in Deleting Entries...?

Errror in Deleting Entries...?

I am trying to write a diary program in c/c++ which can add/view/del
entries. If the delete entry function is called first, it deletes
correctly, but if it is called after adding new entry or viewing an entry,
it doesn't work.
I have gone through the code many times, but I can't find what's wrong.
#include "cosh.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
using namespace std;
struct diar
{
int dd,mm,yy;
char text[10000];
};
void emp();
void dairy();
void nd();
void vd();
void dd();
int main()
{
maxwin();
system("color 0b");
dairy();
return 0;
}
void dairy()
{
bor();
int a=15;int b=5;
g(a,b);b++;
g(a,b);b++;cout<<"_______
"<<endl;
g(a,b);b++;cout<<"\\ ___ `'. .--.
"<<endl;
g(a,b);b++;cout<<" ' |--.\\ \\ |__| .-. .-
"<<endl;
g(a,b);b++;cout<<" | | \\ ' .--. .-,.--. \\ \\ /
/ "<<endl;
g(a,b);b++;cout<<" | | | '| | __ | .-. | \\ \\ / /
"<<endl;
g(a,b);b++;cout<<" | | | || | .:--.'. | | | | \\ \\ / /
"<<endl;
g(a,b);b++;cout<<" | | ' .'| |/ | \\ | | | | | \\ \\ / /
"<<endl;
g(a,b);b++;cout<<" | |___.' /' | |`\" __ | | | | '- \\ `/ /
"<<endl;
g(a,b);b++;cout<<"/_______.'/ |__| .'.''| | | | \\ /
"<<endl;
g(a,b);b++;cout<<"\\_______|/ / / | |_ | | / /
"<<endl;
g(a,b);b++;cout<<" \\ \\._,\\ '/ |_| |`-' /
"<<endl;
g(a,b);b++;cout<<" `--' `\" \'..\'
"<<endl;
g(9,b);b++;cout<<"-------------------------------------------------------------"<<endl;
g(9,b);cout<<"-------------------------------------------------------------"<<endl;
char dch;
do
{
g(32,25);cout<<"[N]ew Entry"<<endl;
g(32,27);cout<<"[V]iew Entries"<<endl;
g(32,29);cout<<"[D]elete Entries"<<endl;
g(32,31);cout<<"[B]ack to Menu"<<endl;
g(32,33);
cin>>dch;
switch(dch)
{
case 'N':
case 'n':nd();break;
case 'd':
case 'D':dd();emp();
break;
case 'v':
case 'V':vd();break;
case 'B':
case 'b':break;
default :cout<<"Enter correct choice";
}
}while((dch)!=('b')&&((dch)!='B'));
}
void nd()
{
emp();
struct diar d;
g(10,20);
cout<<"Enter Date"<<endl;
g(10,21);
cout<<"dd mm yyyy";
g(10,22);
cin>>d.dd>>d.mm>>d.yy;
g(10,24);
cout<<"Enter What Happened(10lines)";
g(10,26);
int ji=0,ki=0,b=0;
char ch;
for(ki=0;ki<1;ki++)
{
g(10,26+b);b+=2;
for(int ij=10;ij<65;ij++)
{
ch=getche();
if(ch==13){d.text[ji]='\n';ji++;ij=65;}
else{d.text[ji]=ch;ji++;}
}
fflush(stdin);
}
d.text[ji]='\0';
g(20,47);
cout<<"Space over\n";
g(30,48);getch();fflush(stdin);
g(10,40);
int siz;
siz=sizeof(diar);
FILE *di;
di=fopen(".\\data\\diary\\d.dat","ab+");
//fprintf(di,"%d %d %d\n%s",d.dd,d.mm,d.yy,d.text);
fseek(di,0,2);
fwrite(&d,siz,1,di);
emp();
fclose(di);
}
void dd()
{
emp();
diar dt;
int b=1,n;
FILE *di,*di2;
di=fopen(".\\data\\diary\\d.dat","rb");
di2=fopen(".\\data\\diary\\tmp.dat","wb");
rewind(di);
int dcho;
int siz=sizeof(struct diar);
while((fread(&dt,siz,1,di))==1)
{
g(30,b+28);
cout<<b<<")"<<dt.dd<<"/"<<dt.mm<<"/"<<dt.yy;
b++;
//n=ftell(di);
//cout<<"\t"<<n;
}
cin>>dcho;
rewind(di);
int fl;
b=0;int i=0,k=30,count=10;
while((fread(&dt,siz,1,di))==1)
{
b++;
emp();
if (dcho==b)
{
fl=1;
}
else
{
fwrite(&dt,siz,1,di2);
}
}
fclose(di);
fclose(di2);
remove(".\\data\\diary\\d.dat");
rename(".\\data\\diary\\tmp.dat",".\\data\\diary\\d.dat");
g(14,47);
if (fl==1){
cout<<"FILE SUCESSFULLY DELETED..PRESS ANY KEY TO CONTINUE...";
getch();
g(14,47);
cout<<" ";
fflush(stdin);
}
else{
g(20,47);
cout<<"NO SUCH FILE EXIST. PRESS ANY KEY TO CONTINUE...";
getch();
fflush(stdin);
}
}
void vd()
{
emp();
diar dt;
int b=1;
int fl=0;
FILE *di;
di=fopen(".\\data\\diary\\d.dat","rb");
rewind(di);
int dcho;
int siz=sizeof(struct diar);
while((fread(&dt,siz,1,di))==1){
g(30,b+28);
cout<<b<<")"<<dt.dd<<"/"<<dt.mm<<"/"<<dt.yy;
b++;
fl=1;
}
if(fl==0){
g(20,47);
cout<<"No Entries Exist Currently .....";
getch();
fflush(stdin);
}
if (fl==1){
cin>>dcho;
rewind(di);
b=0;int i=0,k=30,count=10;
while((fread(&dt,siz,1,di))==1){
b++;
emp();
if (dcho==b){
char ch='A';
g(10,28);
cout<<b<<")"<<dt.dd<<"/"<<dt.mm<<"/"<<dt.yy;
g(10,30);
while((ch)!='\0'){
ch=dt.text[i];
if(ch=='\n'){
k=k+2;
g(10,k);
}
else{
if (count < 65){
cout<<ch;count++;
}
else{
count=10;
k=k+2;
g(10,k);
cout<<ch;
count++;
}
}
i++;
}
getch();
fflush(stdin);
}
}
}
emp();
}
void emp()
{
int a=10,b=20;
while(b<50){
g(a,b);cout<<"
"<<endl;b++;
g(a,b);cout<<"
"<<endl;b++;
g(a,b);cout<<"
"<<endl;b++;
g(a,b);cout<<"
"<<endl;b++;
}
}

No comments:

Post a Comment