0

I wrote an encryption in C++ and the target is java .class file.
I wrote an sample code to test IO.

#include <iostream>    
using namespace std;
#define Length 600
int main () {

  FILE* fp=fopen("a.class","r");
  FILE* save=fopen("b.class","w");
  while(!feof(fp))
  {
      char a[Length];
      fgets(a,Length,fp);
      fprintf(save,"%s",a);
  }
  return 0;
}

a.class looks like below in Sublime

cafe babe 0000 0034 0057 0700 0201 002a 7477 2f63 6f6d 2f6d 696e 6463 7261 6674

And this is my result

Êþº¾/Logger;footerFormlogFormtmle.html/examples/notifications.htmlSourceFile³

So, how to read it properly in c++?

Vishaal Shankar
  • 1,648
  • 14
  • 26
  • See why `while(!feof(fp))` is wrong [**here**](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Start with that. Regardless, expecting printable data from a pcode .class file is a bit optimistic. – WhozCraig May 25 '18 at 08:50
  • 3
    I guess `.class` files are binary files? If so, you should open read and write files in binary mode. Also, why not use C++ `std::fstream` instead of C `FILE*`? – Yksisarvinen May 25 '18 at 08:51
  • Except for the header (not even the correct one, BTW), and `using namespace std;`, this is pure C code. If you want to write C++, use C++ mechanisms. If you want to write C, don't write C++. – Angew is no longer proud of SO May 25 '18 at 08:56
  • The ASCII representation of the hex string in your question is `Êþº¾4W*tw/com/mindcraft`. You should be more specific about what you're trying to achieve and what output you want to see. – DarthJDG May 25 '18 at 08:57
  • @Yksisarvinen Thanks. std::fstream helps. – Jacob Chang May 25 '18 at 09:01

0 Answers0