9 Replies - 1844 Views - Last Post: 18 June 2012 - 12:49 PM Rate Topic: -----

#1 kjhanjee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

I made a image compression program, but it failed

Posted 17 June 2012 - 11:10 AM

I was working on a small personal project (just for doing it)
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.

Is This A Good Question/Topic? 0
  • +

Replies To: I made a image compression program, but it failed

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I made a image compression program, but it failed

Posted 17 June 2012 - 11:19 AM

Try typing the full path of the file to see if your compiler can detect it.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: I made a image compression program, but it failed

Posted 17 June 2012 - 11:55 AM

Also remember that Tubro-Crap uses the DOS 8.3 file naming convention. So if your program is located somewhere like "Program Files" you will find it difficult to open unless you are in the same directory.
Also in the following snippet:

char* nm;
 cout<<"\nEnter the name of the image with extension :  ";
 cin>>nm;

Where are you allocating memory to hold your C-string?

Jim

This post has been edited by jimblumberg: 17 June 2012 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 kjhanjee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: I made a image compression program, but it failed

Posted 17 June 2012 - 10:55 PM

View Postjimblumberg, on 17 June 2012 - 11:55 AM, said:

Also remember that Tubro-Crap uses the DOS 8.3 file naming convention. So if your program is located somewhere like "Program Files" you will find it difficult to open unless you are in the same directory.
Also in the following snippet:

char* nm;
 cout<<"\nEnter the name of the image with extension :  ";
 cin>>nm;

Where are you allocating memory to hold your C-string?

Jim

I did not allocate memory directly initialized it :P and yes my file is in the same director as c:\tc\bin folder but still gives me an error.
Was This Post Helpful? 0
  • +
  • -

#5 kjhanjee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: I made a image compression program, but it failed

Posted 17 June 2012 - 11:20 PM

I tried using the full path method but was unsuccessful, and also allocated memory using new keyword.. like
nm = new char[100];


I still get the same error "Unable to open file"
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: I made a image compression program, but it failed

Posted 18 June 2012 - 02:14 AM

I hope that while typing the full name, you wrote it like
file.open("Folder//Sub_Folder//File.extension");


If yes, then try opening any other file. If it opens other files but not this one, there is some problem with the file.
Was This Post Helpful? 0
  • +
  • -

#7 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: I made a image compression program, but it failed

Posted 18 June 2012 - 06:07 AM

Don't use a cstring. Use a string and if you need to access the raw string then use the attribute to access it.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: I made a image compression program, but it failed

Posted 18 June 2012 - 06:22 AM

View Postkjhanjee, on 18 June 2012 - 01:20 AM, said:

I tried using the full path method but was unsuccessful, and also allocated memory using new keyword.. like
nm = new char[100];


I still get the same error "Unable to open file"


Please post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 kjhanjee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: I made a image compression program, but it failed

Posted 18 June 2012 - 06:57 AM

@JIM I did not change anything in the code just added that little new fella after i had declared he nm string.The whole code remains the same otherwise.

@Stackoverflow I doubt that turbo c3 compiler has support for 'string' type data type as in new compilers and only has cstring type.

I will try writing the code in another compiler thank you everyone for your help.
Was This Post Helpful? 0
  • +
  • -

#10 kjhanjee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: I made a image compression program, but it failed

Posted 18 June 2012 - 12:49 PM

Please someone help me convert this code to dev c++ code. I am unable to include half of the adders ad the ios::no create also gives an error. I need some one who is ready to help me with my project.
And please someone suggest me some good tutorials for gnu c++ compiler
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1