I am writing a code for arithmetic compression. But my problem is i can't open the file 2nd time for reading.

code is:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>
using namespace std;
#define MAX_SYM 255

struct node
{
      char ch;
      int count;
      /*public:
      node()
      {
            //cout<<"constructor at work. \n";
            count =0;
            next= NULL;
      }
      void count_incre()
      {
           count++;
      }*/
}pq[MAX_SYM];

//imported code
/* global variables, a necessary evil */
int nbits, current_byte, nbytes;

/* output a single byte to an open file when 8 bits are accumulated*/
void bitout (FILE *f, char b) {

cout<<"in fxn bitout";
/* shift current byte left one */
current_byte<<=1;                   //current_byte = current_byte <<  1;
cout<<"shifted";

/* put a one on the end of this byte if b is '1' */
if (b == '1') 
current_byte|=1;
cout<<"and";                     //current_byte= current_byte | 1;       

// one more bit 
nbits++;
cout<<"nbits: "<<nbits;
system("pause");

/* enough bits? write out the byte */
if (nbits == 8) {
          cout<<"nbits=8; writing" ;
   fputc (current_byte, f);
   nbytes++;
   nbits = 0;
   current_byte = 0;
   }
   cout<<"returning";
   system("pause");
}

void ini_count(ifstream& fin_t)
{
    char ch; 
    fin_t>>ch;
    cout<<"Character read is: "<<ch;
    system("pause");
    while (fin_t)
    {
         pq[ch].count++;
         fin_t>>ch;
         
         cout<<"Character read is: "<<ch;
         system("pause");
    }
}
// global variables
int sym_count=0, TOTAL_COUNT=0;
void count_tot()
{
    for(int i=0; i< MAX_SYM; i++)
    {
             if(pq[i].count!=0)
             {
                         sym_count++;
                         TOTAL_COUNT += pq[i].count;
             }
    }
}

int cum_count(int sym)
{
     int count_temp;
     for ( int i=0; i<=sym; i++)
     {
         if (pq[i].count != 0)
         {
               count_temp += pq[i].count;
         }
     }
     return count_temp;
}
     
int main()
{ 
  ifstream fin;
  char file_in[100];
  cout<<"Enter name of file: ";
  gets(file_in);
  fin.open(file_in, ios::in);
  if (!file_in)
  {
        cout<<"File not found, exiting....";
        exit(0);
  }
  ini_count(fin);
  count_tot();
  cout<<"TOTAL_COUNT = "<<TOTAL_COUNT<<endl;
  
  for(int i=0; i<MAX_SYM; i++)
    {
             if(pq[i].count!=0)
             {
                    cout<<(char)i<<" :  "<<pq[i].count<<endl;
             }
    }
    system("pause");
  
  unsigned short u=0xffff, l=0, scale_3=0;
  
  /* initialize globals for bitout() */
  current_byte = 0;
  nbits = 0;
  nbytes = 0;
  
  char a;  
  //fin.open(file_in, ios::in);
  //cout<<"at posn: "<<fin.tellg();
  fin.seekg(0,ios::beg);
  //cout<<"at posn: "<<fin.tellg();
  fin>>a;                                     //[I][B]CAN'T OPEN HERE[/B][/I]
  if (!fin)
  {
           cout<<"file couldn't be opened.";
           system("pause");
           exit(0);
  }
  
  cout<<"character read is : "<<a<<endl;
  FILE *fout;
  fout = fopen ("ari_comp.txt", "w");
  cout<<"file opened";

  do
  {
    cout<<"cum_count: "<<cum_count(a)<<endl;
    unsigned int range=(int)(u-l+1);                                    //because 65535-0 = 65536 which is out of range of short  
    cout<<"Range= "<<range<<endl;
    u= l - 1 + (unsigned short)((range*cum_count(a))/TOTAL_COUNT);
    l= l + (unsigned short)((range*cum_count(a-1))/TOTAL_COUNT);
    cout<<"u: "<<u<<endl;
    cout<<"l: "<<l<<endl;
    
    unsigned short x= l&0x8000;
    unsigned short y= u&0x8000;
    unsigned short midu=u&0x4000;
    unsigned short midl=l&0x4000;
    midu=midu>>30;
    midl=midl>>30;
    
    while(x==y ||(midu==0 && midl==1) )
    {
         if(x==y)
         {
             cout<<"\n x=y";
             unsigned short b= x>>15;                    // msb becomes the lsb    
             //b=MASK_BIT(0);                    // to remove the effects of ARHL if it happened
             l<<=1;
             u<<=1;
             u |=1;
             bitout(fout,b);
             while(scale_3 > 0)
             {
                    bitout(fout,~b);
                    scale_3--;
             }
         }
         else if (midu==0 && midl==1)
         {   
             cout<<"\n E3 mapping";                                      
              l<<=1;
              l= l^0x8000;                                    //to complement use xor a^1 = ~a       
              u<<=1;
              u= u^0x8000;
              u |=1;   
              scale_3++;
         }
    }   
  fin>>a;
  }while (fin);
  
    cout<<"\n Read whole file";
    system("pause");
    
    unsigned int tag= (u+l)/2;
    string s =*(string *)(&tag);             //typecasting
    //int tag_bits=ceil( -1*log(P(x)) ) +1;
    
    bitout(fout,s[0]);
    if (scale_3>0)
    {
         int b=s[0];
         b=b^0x0001;
         while(scale_3!=0)
         {
               bitout(fout,b);
               scale_3--;
         }
    }
    
    for (int i=1; i<<15; i++)                                        //for (int i=1; i<<tag_bits; i++)
    {
        bitout(fout,s[i]);
    }
    /* finish off the last byte */
    while (nbits)
    {                                        //this loop will continue until 8 bits aren't made which will be written to file
      bitout (fout, '0');
    }
    system("pause");
    return 0;
}

O/p is this:
Enter name of file: z.txt
Character read is: pPress any key to continue . . .
Character read is: rPress any key to continue . . .
Character read is: aPress any key to continue . . .
Character read is: kPress any key to continue . . .
Character read is: aPress any key to continue . . .
Character read is: sPress any key to continue . . .
Character read is: hPress any key to continue . . .
Character read is: hPress any key to continue . . .
TOTAL_COUNT = 7
a : 2
h : 1
k : 1
p : 1
r : 1
s : 1
Press any key to continue . . .
file couldn't be opened.Press any key to continue . . .

Recommended Answers

All 4 Replies

I know its already open. why am i not being able to read it from start??
I also tried to close it before opening and then open again but that didn't work also.

here is ashorter code:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>
using namespace std;
#define MAX_SYM 255

struct node
{
      char ch;
      int count;
      /*public:
      node()
      {
            //cout<<"constructor at work. \n";
            count =0;
            next= NULL;
      }
      void count_incre()
      {
           count++;
      }*/
}pq[MAX_SYM];


void ini_count(ifstream& fin_t)
{
    char ch; 
    fin_t>>ch;
    cout<<"Character read is: "<<ch;
    system("pause");
    while (fin_t)
    {
         pq[ch].count++;
         fin_t>>ch;
         
         cout<<"Character read is: "<<ch;
         system("pause");
    }
}
// global variables
int sym_count=0, TOTAL_COUNT=0;
void count_tot()
{
    for(int i=0; i< MAX_SYM; i++)
    {
             if(pq[i].count!=0)
             {
                         sym_count++;
                         TOTAL_COUNT += pq[i].count;
             }
    }
}

     
int main()
{ 
  ifstream fin;
  char file_in[100];
  cout<<"Enter name of file: ";
  gets(file_in);
  fin.open(file_in, ios::in);
  if (!file_in)
  {
        cout<<"File not found, exiting....";
        exit(0);
  }
  ini_count(fin);
  count_tot();
  cout<<"TOTAL_COUNT = "<<TOTAL_COUNT<<endl;
  
  for(int i=0; i<MAX_SYM; i++)
    {
             if(pq[i].count!=0)
             {
                    cout<<(char)i<<" :  "<<pq[i].count<<endl;
             }
    }
    system("pause");
  
  unsigned short u=0xffff, l=0, scale_3=0;
  
  /* initialize globals for bitout() */
  current_byte = 0;
  nbits = 0;
  nbytes = 0;
  
  char a;  
  //fin.open(file_in, ios::in);
  //cout<<"at posn: "<<fin.tellg();
  fin.seekg(0,ios::beg);
  //cout<<"at posn: "<<fin.tellg();
  fin>>a;
  if (!fin)
  {
           cout<<"file couldn't be opened.";
           system("pause");
           exit(0);
  }
  
  cout<<"character read is : "<<a<<endl;
  FILE *fout;
  fout = fopen ("ari_comp.txt", "w");
  cout<<"file opened";

  do
  {
    //something
    }   
  fin>>a;
  }while (fin);
  
    system("pause");
    return 0;
}

I know its already open. why am i not being able to read it from start??
I also tried to close it before opening and then open again but that didn't work also.

here is ashorter code:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>
using namespace std;
#define MAX_SYM 255

struct node
{
      char ch;
      int count;
      /*public:
      node()
      {
            //cout<<"constructor at work. \n";
            count =0;
            next= NULL;
      }
      void count_incre()
      {
           count++;
      }*/
}pq[MAX_SYM];


void ini_count(ifstream& fin_t)
{
    char ch; 
    fin_t>>ch;
    cout<<"Character read is: "<<ch;
    system("pause");
    while (fin_t)
    {
         pq[ch].count++;
         fin_t>>ch;
         
         cout<<"Character read is: "<<ch;
         system("pause");
    }
}
// global variables
int sym_count=0, TOTAL_COUNT=0;
void count_tot()
{
    for(int i=0; i< MAX_SYM; i++)
    {
             if(pq[i].count!=0)
             {
                         sym_count++;
                         TOTAL_COUNT += pq[i].count;
             }
    }
}

     
int main()
{ 
  ifstream fin;
  char file_in[100];
  cout<<"Enter name of file: ";
  gets(file_in);
  fin.open(file_in, ios::in);
  if (!file_in)
  {
        cout<<"File not found, exiting....";
        exit(0);
  }
  ini_count(fin);
  count_tot();
  cout<<"TOTAL_COUNT = "<<TOTAL_COUNT<<endl;
  
  for(int i=0; i<MAX_SYM; i++)
    {
             if(pq[i].count!=0)
             {
                    cout<<(char)i<<" :  "<<pq[i].count<<endl;
             }
    }
    system("pause");
  
  unsigned short u=0xffff, l=0, scale_3=0;
  
  /* initialize globals for bitout() */
  current_byte = 0;
  nbits = 0;
  nbytes = 0;
  
  char a;  
  //fin.open(file_in, ios::in);
  //cout<<"at posn: "<<fin.tellg();
  fin.seekg(0,ios::beg);
  //cout<<"at posn: "<<fin.tellg();
  fin>>a;
  if (!fin)
  {
           cout<<"file couldn't be opened.";
           system("pause");
           exit(0);
  }
  
  cout<<"character read is : "<<a<<endl;
  FILE *fout;
  fout = fopen ("ari_comp.txt", "w");
  cout<<"file opened";

  do
  {
    //something
    }   
  fin>>a;
  }while (fin);
  
    system("pause");
    return 0;
}

I'm not sure why you posted this 3 times, but it's really not an advisable habit to get in to...

In ini_count(), you appear to read to the end of the file. Because of that, and since I don't see any sort of reset, I'm guessing your eofbit flag is still set. While that flag is set, you can not read from the file. Try this:

char a; 
  //fin.open(file_in, ios::in);
  //cout<<"at posn: "<<fin.tellg();
  fin.seekg(0,ios::beg);
  if (!fin.good())
    fin.clear(); 
  //cout<<"at posn: "<<fin.tellg();
  fin>>a;
  if (!fin)
  {
           cout<<"file couldn't be opened.";
           system("pause");
           exit(0);
  }

thanks. i will try this.
actually 2nd time i posted a shorter code so that people may understand easily. I got double posted for reasons i don't know. Not INTENTIONAL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.