-2

The two functions below are about to implement zip and unzip, but I have problems selecting the correct type buffer to save the string from the file.

Also, for char* ptr = line, which is declared a pointer to point to the string line, it returns me an error.

Unzip function is the kinda same problem.

Error :error: cannot convert ‘std::__cxx11::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘char*’ in initialization

//zip function
int main(int argc, char*argv[])
{
  if(argc == 1){
    return 0;
  }
  string line;
  // char *line = new char[SIZE];
  for(int i = 1; i < argc; i++){
    ifstream file(argv[i]);
    if(file.fail()){
      cerr << "wzip: cannot open file" << endl;
      return 1;
    }
    while(getline(file,line)){
      char *ptr = line;
      char current = *ptr;
      unsigned int count = 0;
      while(*ptr){
        if(*ptr == current){
          count++;
          ptr++;
          continue;
        }else if(count == 4294967295){
          cout << count << current;
          current = 0;
          count = 0;
        }else{
          cout << count << current;
          current =*ptr;
          count = 0;
        }
      }
    }
    cout << endl;
    file.close();
  }
  return 0;
}
//unzip function
int main(int argc, char* argv[])
{
  if(argc == 1){
    return 0;
  }
  char buffer;
  for(int i = 1; i < argc; i++){
    ifstream file(argv[i]);
    if(file.fail()){
      cerr <<"wunzip: cannot open file" << endl;
      return 1;
    }
    while(getline(file,line)){
      char *ptr = buffer;
      unsigned int count = 0;
      while(*ptr != 0){
        if(*ptr <='9' && ptr >= 0){
          count = count*10+(*ptr-'0');
        }else{
          while(count--){
            cout << *ptr;
            count = 0;
          }
        }
        ptr++;
      }
    }
    cout << endl;
    file.close();
  }
  return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This will fail badly if your input file contains any digits, spaces, or control characters (such as newline)... – Chris Dodd Jan 14 '21 at 21:32

1 Answers1

1

This line is the problem, as it tries to set a char * equal to a std::string, which is not an allowed operation (because a std::string is a C++ object, not a raw character-array):

char *ptr = line;

you probably want something more like this, that sets ptr to point to the beginning of line's internal char-array:

const char *ptr = line.c_str();

(I added const to the type of ptr because you're not allowed to write into the character-array that is held internally by a std::string)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Thank's that fix the char* line problem, but when I compile it and run it. the test file contains aabbccdd, after zip and unzip, it returned abc. – haonan wang Jan 14 '21 at 22:13
  • 1
    @haonanwang Then you need to learn how to debug your code so you can see where its behavior differs from what you are expecting. See [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Remy Lebeau Jan 14 '21 at 22:18