Search code examples
c++binaryifstreamdecompilerofstream

Removing groups of "000" from my file?


In C++ I have made a program that exports to binary and now I am making a reader. It reads correctly, but there is only 1 issue. My file is a file that contains a set of numbers and when it is read and printed to the screen you see, 1470009300047000199. The sets of 3 "000" isn't supposed to be there. I loaded this file using an ifstream and plan to keep it that way. Can someone tell me how to remove the sets of "000" in my file? If I have to write another C++ program that does that I am fine with it, I just need something to remove the "000" and replace it with a space.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        cout << "Error 1";
        return 0;
    }
    else
    {
        int FileLength;
        ifstream InputFile(argv[1], ios::binary);
        ofstream OutputFile("DECOMPILED_FILE.txt");
        InputFile.seekg(0, ios::end);
        FileLength = InputFile.tellg();
        InputFile.seekg(0, ios::beg);
        for (int i = 0; i < FileLength; i++)
        {
            cout << InputFile.get();
        }

        cin.get();
    }
    return 0;
}

Solution

  • How about a regular expression ? Try finding the substring '000' on the file, if found, replace it by " ". Pseudocode:

    for each line in the file do:
         if line.strstr("000") then
               line.replace("000", " ")
    
          cout << line << endl;