-2
\$\begingroup\$

I am an aspiring C++ programmer and I have been programming for a few years off and on but I don't have much usable knowledge. Right now I am working on filesystem safety so I decided to write an erasure (think BleachBit).

It needs to be run from terminal as such (root/ program filename.something) and needs to open the file in memory, get file size, write in raw binary format to each bit of file, close file,. This is a part where I am stuck. It needs to delete the (I am pretty sure it's some kind of pointer) object that represents the file that the system stores by wiping it from memory.

ren.cpp:

#include <stdio.h>
#include <string>






void bitwrite(int pass, char * fn)
{
    FILE * fp;
    fp = fopen(fn, "wb");
    int k;
    const void * kptr;
    switch(pass)
    {
        case 0:
            k = 0b11111111111111111111111111111111;
            break;
        case 1:
            k = 0b10101010101010101010101010101010;
            break;
        case 2:
            k = 0b01010101010101010101010101010101;
            break;
        case 3:
            k = 0b11111111111111111111111111111111;
            break;
        case 4:
            k = 0b10101010101010101010101010101010;
            break;
        case 5:
            k = 0b01010101010101010101010101010101;
            break;
        case 6:
            k = 0b00000000000000000000000000000000;
            break;
    }
    fseek(fp, 0, SEEK_END);
    off_t size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    for(int a = 0; a<sizeof(size);a++)
    {   
        fseek(fp, a,  SEEK_SET);
        fwrite(&k, sizeof(int), 1, fp);

    }
    fclose(fp);
}

int main(int argc, char * argv[])
{

    if(argv == NULL)
    {
        printf("usage: ren file to be destroyed");
        return 1;
    }
    for(int i=0;i<7;i++)
    {
        bitwrite(i, argv[1]);

    }
    printf("success!");

    return 0;
}

This code needs to run efficiently as possible. I am at a loss as how to improve and fix errors. Currently, C++ ren.cpp -o ren runs without error, but gdb throws a sigseg error @fseek.

\$\endgroup\$
  • \$\begingroup\$ You forgot to set fp before you call fseek. Did you mean for the result of the call to fopen to be set to fp? \$\endgroup\$ – jbapple Jun 5 '18 at 0:25
  • \$\begingroup\$ i think so? im not fully familiar with file io \$\endgroup\$ – user171218 Jun 5 '18 at 0:34
  • 1
    \$\begingroup\$ Then there's the effects of caching by the OS., And the (remote) possibility that when you open the file later it might not be written to the same physical location on the disk. Either one will keep your scheme from working. \$\endgroup\$ – 1201ProgramAlarm Jun 5 '18 at 0:36
  • 2
    \$\begingroup\$ The multi-pass thing does not help on modern drives. One pass of random data is sufficient. And it might not work at all, if sector remapping takes place! \$\endgroup\$ – JDługosz Jun 5 '18 at 1:25
  • 1
    \$\begingroup\$ You can not do what you are trying to do from with file system calls (that is a layer of abstraction too far away). The file interface provided by the OS does not give you that much control of how it interacts with the blocks on the disk. You need to look at the block interface provided by your hardware. \$\endgroup\$ – Martin York Jun 5 '18 at 15:06
2
\$\begingroup\$

Instead of switch, just use an array.

k= pattern[pass];

Don’t use the C macro NULL for any purpose. Banish it from your memory!


The style in C++ is to put the * or & with the type, not the identifier. This is called out specifically near the beginning of Stroustrup’s first book, and is an intentional difference from C style.


Why are you including <string> when you do not use any? There is nothing at all from the C++ standard library here. I would think it was a C program.

\$\endgroup\$
  • \$\begingroup\$ <string>is from my last program, can you drop a link to why null is bad? can you pass on info as to that book and to 'c style'? \$\endgroup\$ – user171218 Jun 5 '18 at 1:41
  • \$\begingroup\$ also i need to find an efficient way to unlink the file, truncate it, clear the sys cache and avoid sector remapping? if you have any advice on that please let me know. \$\endgroup\$ – user171218 Jun 5 '18 at 1:43
  • \$\begingroup\$ @user171218 not in this Stack. this is for code reviews. \$\endgroup\$ – JDługosz Jun 5 '18 at 1:45
  • \$\begingroup\$ Please don't answer questions that are off-topic here. This encourages others to continue asking off-topic questions. Instead, you should vote to close. \$\endgroup\$ – Daniel Jun 5 '18 at 5:50
  • \$\begingroup\$ @user171218 start with The Cpre Guidelines. I was saying write myclass* p; with the * hugging the type name. \$\endgroup\$ – JDługosz Jun 6 '18 at 0:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.