I wrote the following code
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class app
{
public: void run();
void exita();
};
class workonimage
{
public : void runcompress(char* nm);
void rundecompress(char* nm);
private : void compress(char* nm);
void decompress(char* nm);
};
void app::run()
{
clrscr();
workonimage w;
int ch1;
char ch='y';
char* nm;
cout<<"\nEnter the name of the image with extension : ";
cin>>nm;
do
{
clrscr();
cout<<"What do u want to do run : ";
cout<<"\n1.) Compressor";
cout<<"\n2.) Decompressor\n";
cin>>ch1;
switch(ch1)
{
case 1: w.runcompress(nm);
break;
case 2: w.rundecompress(nm);
break;
default: cout<<"\nWrong choice input";
cout<<"\nDo you want to continue y or n : ";
cin>>ch;
}
}
while(ch=='y');
}
void app::exita()
{
exit(0);
}
void workonimage::runcompress(char* nm)
{
clrscr();
compress(nm);
cout<<"\nNote: Compressed file will not look as the orginal image they are jst for
increasing transportability of the file. The image will have to be decompressed before
opening";
}
void workonimage::rundecompress(char* nm)
{
clrscr();
decompress(nm);
cout<<"Image is decompressed and ready to be opened";
}
void workonimage::compress(char* nm)
{
fstream f;
char* a;
f.open(nm,ios::out|ios::nocreate);
if(!f)
{
cout<<"Unable to open file";
getch();
exit(0);
}
int i=0;
f.seekg(0);
while(!f.eof())
{
f.read((char*) a[i],sizeof(char));
i++;
}
f.open(nm,ios::in|ios::nocreate);
f.seekp(0);
i=0;
while(!f.eof())
{
while(i!=sizeof(a))
{
int k=0;
int j=i+1;
while(j!=sizeof(a))
{
if(a[i]==a[j])
{
k++;
}
j++;
}
f.write((char*)a[i],sizeof(char));
f.write((char*)'#',sizeof(char));
f.write((char*)k,sizeof(int));
f.write((char*)'#',sizeof(char));
if(k==0)
i=i++;
else
i=i+k;
}
}
f.close();
}
void workonimage::decompress(char* nm)
{
fstream f2;
char* a2;
f2.open(nm,ios::out|ios::nocreate);
if(!f2)
{
cout<<"Unable to open file";
getch();
exit(0);
}
int i=0;
f2.seekg(0);
while(!f2.eof())
{
f2.read((char*)a2[i],sizeof(char));
i++;
}
f2.open(nm,ios::in|ios::nocreate);
i=0;
f2.seekp(0);
while(!f2.eof())
{
if(a2[i+1]=='#')
{
for(int k=0;k<a2[i+2];k++)
f2.write((char*)a2[i],sizeof(char));
}
i++;
}
f2.close();
}
void main()
{
clrscr();
app a;
int ch2;
char ch3;
do
{
clrscr();
cout<<"\nWhat do you want to do with the program : ";
cout<<"\n1.) Run Program";
cout<<"\n2.) Exit \n";
cin>>ch2;
switch(ch2)
{
case 1: a.run();
break;
case 2: a.exita();
break;
default: cout<<"\nWrong chice input ";
cout<<"\nDo you want to continue y or n : ";
cin>>ch3;
}
}
while(ch3=='y');
getch();
}
whenever I try to open a file, say namesign.jpg, which already exists in the same directory as my compiler, I always get the error "Unable to create file"
I use turbo c3 compiler. I know its old, no gui and stuff. I don't need suggestion about the compiler but please help me out with the code.

New Topic/Question
Reply



MultiQuote






|