i trying to input the text from a text file into a byte buff[]. When i did this byte buf1[] = word; it will prompt me with "initializer fails to determine size of `buf1' " and invalid initializer error. How am i able to pass in the text of my text file into byte buf1[]?

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <fstream>

using namespace std;
 
typedef unsigned char byte;
 
byte *rotlexcmp_buf = NULL;
int rottexcmp_bufsize = 0;
 
int rotlexcmp(const void *l, const void *r)
{
        int li = *(const int*)l,
                ri = *(const int*)r,
                ac = rottexcmp_bufsize;
 
        if (li == ri) return 0;
 
        while (rotlexcmp_buf[li] == rotlexcmp_buf[ri])
        {
                li = (li + 1) % rottexcmp_bufsize;
                ri = (ri + 1) % rottexcmp_bufsize;
                if (--ac == 0) return 0;
        }
 
        return (rotlexcmp_buf[li] > rotlexcmp_buf[ri]) ? 1 : -1;
}
 
void bwt_encode(byte *buf_in, byte *buf_out, int size, int *primary_index)
{
        int indices[size];
        int i;
 
        for (i = 0; i < size; i++)
                indices[i] = i;
 
        rotlexcmp_buf = buf_in;
        rottexcmp_bufsize = size;
        qsort(indices, size, sizeof(int), rotlexcmp);
 
        for (i = 0; i < size; i++)
        {
                buf_out[i] = buf_in[(indices[i] + size - 1) % size];
                if (indices[i] == 0) *primary_index = i;
        }
}
 
void bwt_decode(byte *buf_in, byte *buf_out, int size, int primary_index)
{
        int buckets[256];
        int i,j,sum;
        int indices[size];
 
        memset(buckets, 0, sizeof(buckets));
 
        for (i = 0; i < size; i++)
        {
                indices[i] = buckets[buf_in[i]];
                buckets[buf_in[i]]++;
        }
 
        sum = 0;
        for (i = 0; i < 256; i++)
        {
                register int __t = buckets[i];
                buckets[i] = sum;
                sum += __t;
        }
 
        j = primary_index;
        for (i = size - 1; i >= 0; i--)
        {
                buf_out[i] = buf_in[j];
                j = buckets[buf_in[j]] + indices[j];
        }
}
 
int main()
{
        ifstream fin;
        fin.open("Dec of Ind.txt");
        string word;
        
        while(getline(fin,word))
        {
              //cout<<word;
              byte buf1[] = word;
              int size = strlen((const char*)buf1);
              byte buf2[size];
              byte buf3[size];
              int primary_index;
 
              bwt_encode(buf1, buf2, size, &primary_index);
              bwt_decode(buf2, buf3, size, primary_index);
 
              assert(!memcmp (buf1, buf3, size));
              printf("Result is the same as input, that is: <%.*s>\n", size, buf3);
              //Print out encode/decode results: 
              printf("Input  : <%.*s>\n", size, buf1);
              printf("Encoded: <%.*s>\n", size, buf2);
              printf("Decoded: <%.*s>\n", size, buf3);                  
        }
        cout<<endl;
        /*
        byte buf1[] = "Polska Wikipedia";
        int size = strlen((const char*)buf1);
        byte buf2[size];
        byte buf3[size];
        int primary_index;
 
        bwt_encode(buf1, buf2, size, &primary_index);
        bwt_decode(buf2, buf3, size, primary_index);
 
        assert(!memcmp (buf1, buf3, size));
        printf("Result is the same as input, that is: <%.*s>\n", size, buf3);
        // Print out encode/decode results: 
        printf("Input  : <%.*s>\n", size, buf1);
        printf("Encoded: <%.*s>\n", size, buf2);
        printf("Decoded: <%.*s>\n", size, buf3);  
        */
        fin.close();
        system("pause");
        return 0;
}
#include <string>
#include <vector>
#include <fstream>

typedef unsigned char byte ;

void bwt_encode( byte* buf_in, byte* buf_out, int size, 
                 int* primary_index ) ;
void bwt_decode( byte* buf_in, byte* buf_out, int size, 
                 int primary_index ) ;

int main()
{
  std::ifstream fin("Dec of Ind.txt") ;
  std::string word ;

  while( std::getline(fin,word) )
  {
    std::vector<byte> vec1( word.begin(), word.end() ) ;
    std::vector<byte> vec2( vec1.size() ), vec3( vec1.size() ) ;
    int primary_index = 0 ;
    byte* buf1 = &vec1.front() ;
    byte* buf2 = &vec2.front() ;
    byte* buf3 = &vec3.front() ;
    int size = vec1.size() ;
    
    bwt_encode( buf1, buf2, size, &primary_index ) ;
    bwt_decode( buf2, buf3, size, primary_index ) ;

    // etc
  }
  // ...  
}

thanks vijayan121 the program works!!

But when i run it it prompt me an microsoft error box. Could it due to my txt file is too big? i try to increase the buffer size but i still get the error box. What can i do to get rid of it?

my text file is 9kb.

> ... the program works!!
it should not even compile. a conforming compiler will give you an error for this:

void bwt_encode(byte *buf_in, byte *buf_out, int size, int *primary_index)
{
  // error: size is not a constant known at compile-time
  int indices[ size ];
  // ...
}

> ... microsoft error box. Could it due to my txt file is too big?
no. your file is very small.
the problem is here:

// ...
//Print out encode/decode results: 
printf("Input  : <%.*s>\n", size, buf1);
printf("Encoded: <%.*s>\n", size, buf2);
printf("Decoded: <%.*s>\n", size, buf3); 
// ...

printf (with %s) can only handle c-style strings (char arrays which are null-terminated).
buf1, buf2, buf3 are char arrays alright; but they do not have a null character at the end.

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.