title: Read the binary content of a file and reverse its content in C++
body: <p>I created a simple program that can read a text file line by line, then reverse itss content, I want to make the same thing with other file extension like (zip,rar,jpg, doc, pdf...) My real intention is to create a file "corrupter/de-corrupter" program, by reversing the binary content of the file, so it can not be opened by acrobat or winzip etc...</p>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li><p>When the file is simple text, I can read and print all the lines, when it is a pdf or zip, it only prints a word or two of gibberish binary data, how can I get all the lines, and print them?</p></li>
  <li><p>CodeBlocks gives me an error, there is a problem with converting from string to char, how to fix this problem?</p></li>
  </ul>
  
  <p>This is what I have so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  
  using namespace std;
  
  fstream file;
  string line;
  
  void reverseLine(char message[]);
  
  void reverseLine(char message[])
  {
      char reversed;
      int i = strlen(message)  -1;
      while (i&gt;=0)
          {
          reversed = reversed + message[i];
          i = i -1;
          }
      cout&lt;&lt;message&lt;&lt;endl;
      cout&lt;&lt;reversed&lt;&lt;endl;
  }
  
  
  int main()
  {
  int numLines=0;
  file.open("g:/example.txt");
  
  if (!file.is_open())
  {
      cout&lt;&lt;"error in opening the file !"&lt;&lt;endl;
  }
  else
      {
          cout&lt;&lt;"success in opening the file !"&lt;&lt;endl;
          cout&lt;&lt;"--------------------------------------"&lt;&lt;endl;
          while (!file.eof()){
                 // for (int i=0; i&lt;100; i++)
                  {
                      cout&lt;&lt;(getline(file,line))&lt;&lt;endl;
                      cout&lt;&lt;line&lt;&lt;endl;
                      //reverseLine(getline(file,line)); //&lt;==dont work :(
                      numLines++;
  
                  }
          }
          cout&lt;&lt;numLines&lt;&lt;endl;
          file.close();
      }
   }
  </code></pre>
  
  <p>After a lot of research, I created some functions "copied some code :p" that can do: 
  - find all the hard drives partitions.
  - search in a drive by file extension
  - open file in binary mode and reverse its content and save the reversed file.</p>
  
  <p>The problem:
  * I want to send the result of " getDrives" to the getFilelisting" function
  and open the files found, reverse them and save after.</p>
  
  <p>what I tried to do:</p>
  
  <p>send the "getDrives" result into an array so I can use it in a for loop. 
  send the found file into the "file.open" but It does not work.
  Here is the new code, I hope someone can help link the functions all together.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  #include &lt;windows.h&gt;
  
  using namespace std;
  
  
  
  
  fstream file;           //file to open
  fstream outFile;        //the reversed file
  string line;            // read lines
  
  int numLines=0;         //number of lines read
  
  
  
  void reverseLine()          //fonction for reversing the line                      
  {
  while (!file.eof())         // read line by line untile the end of the  file                   
  {
  string reversed;
  getline(file,line);
  int i = line.length()  -1;
  while (i&gt;=0)
  {
  reversed = reversed + line[i];
  i = i -1;
  if (i &lt; 0)
  {
  outFile&lt;&lt;reversed&lt;&lt;endl;
  }
  }
  //numLines++;
  }
  }
  
  
  char getDrives()        //find partitions.
              {
              DWORD drives = GetLogicalDrives();
                       for (int i=0; i&lt;26; i++)
                       {
                          if( ( drives &amp; ( 1 &lt;&lt; i ) ) )
                          {
                             //CHAR driveName[] = { TEXT('A') + i, TEXT(':'), TEXT('\\'), TEXT('\0') };
  
                             CHAR driveName[] = { TEXT('A') + i, TEXT('\0')  };
  
                             cout&lt;&lt;driveName&lt;&lt;endl;
  
  
                          }
  
                       }
  
              }
  
  
  
  
   void GetFileListing(string directory, string fileFilter, bool  recursively = true) // fonction that searches in a drive by one file type, I want it to search for all drives with multiple file extensions
   {
   if (recursively)
   GetFileListing(directory, fileFilter, false);
  
   directory = directory + "\\";
  
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
  
   string filter = directory + (recursively ? "*" : fileFilter);
  
   hFind = FindFirstFile(filter.c_str(), &amp;FindFileData);
  
   if (hFind == INVALID_HANDLE_VALUE)
   {
  
   return ;
   }
  
                else
                {
                  if (!recursively)
                  {
                      cout &lt;&lt; directory + string(FindFileData.cFileName) &lt;&lt;  endl;
                  }
  
                  while (FindNextFile(hFind, &amp;FindFileData) != 0)
                  {
                    if (!recursively)
                    {
                      cout &lt;&lt; directory + string(FindFileData.cFileName) &lt;&lt;  endl;
                    }
                    else
                    {
                      if ((FindFileData.dwFileAttributes &amp;     FILE_ATTRIBUTE_DIRECTORY)&gt;0 &amp;&amp; FindFileData.cFileName[0]!='.')
                      {
                         GetFileListing(directory + string(FindFileData.cFileName), fileFilter);
                      }
                    }
                  }
  
                  DWORD dwError = GetLastError();
                  FindClose(hFind);
                  if (dwError != ERROR_NO_MORE_FILES)
                  {
                    cout &lt;&lt; "FindNextFile error. Error is "&lt;&lt; dwError &lt;&lt; endl;
                  }
                }
              }
  
  
  
  
  int main(int argc, char* argv[])
  
              {
                  getDrives();
                  //char allDrives = getDrives();  // dont work
                  //cout&lt;&lt;"number of drives are:" &lt;&lt;sizeof(allDrives)&lt;&lt;endl;
                  //cout&lt;&lt;allDrives&lt;&lt;endl;
  
                  //string fileToOpen;
                  //GetFileListing("e:\\", "*.txt "); // works but for only on drive with on extension
  
                       fstream foundFile; // file found by GetFileListing
  
                      //foundFile.open("e:/foundFiles.txt",fstream::out );   // dont work
                     // foundFile&lt;&lt;(GetFileListing( "e:\\", "*.stl "));       // dont work
  
                    // file.open(GetFileListing( "e:\\", "*.3ds"));           //dont work , open files found by GetFileListing
  
                      //file.open(fileToOpen,ios_base::in |ios_base::binary| ios_base::out); //ne marche pas
                      //GetFileListing("e:\\", "*.txt ");                                     // dont work
  
                      file.open("e:/test.txt", ios_base::in |ios_base::binary| ios_base::out);
                      outFile.open("e:/test_m.txt", ios_base::binary | fstream::out);
  
                      if ( !file.is_open() )
                                      {
                                          cout&lt;&lt;"error in opening the file !"&lt;&lt;endl;
                                          cout&lt;&lt;"--------------------------------------"&lt;&lt;endl;
                                      }
                                      else
                                              {
                                                  cout&lt;&lt;"success in opening the file and outFile !"&lt;&lt;endl;
                                                  cout&lt;&lt;"--------------------------------------"&lt;&lt;endl;
  
                                                  while (!file.eof())
                                                      {
                                                         // for (int i=0;  i&lt;300; i++) // dont work, i want to read the 300 lines only
  
                                                          string reversed; // line found
                                                          getline(file,line);
                                                          int i = line.length()  -1;
                                                          while (i&gt;=0)
                                                              {
                                                              reversed = reversed + line[i];
                                                              i = i -1;
                                                              //cout&lt;&lt;line&lt;&lt;endl; // print found line
                                                               if (i &lt; 0)
                                                                  {
                                                                  //cout&lt;&lt;reversed&lt;&lt;endl;
                                                                  outFile&lt;&lt;reversed&lt;&lt;endl;   //print the reversed line in the output file
                                                                  }
                                                              } // end while
                                                              numLines++;
  
                                                              } // end while
                                                              cout&lt;&lt;"number of lines in the original file are :" &lt;&lt; numLines  &lt;&lt;endl;
                                                             // file.close();
                                                              // std::remove("e:/lines.txt"); //delete the original file
                                                              outFile.close();
  
  
  
                                                  } // end else
                                      } //end main()
  </code></pre>