code for reading binary file.is it correct?

i have the following code for reading bytes from binary file.

CODE

[code]#include
#define BUFFER 1024
//Encrypts a file by flipping all bytes, i.e XOR
int main(int argc, char** argv)
{
char* input, *output;
FILE* fin, *fout;
void* buffer;
size_t read;
if (argc != 3)
return 1;
input = argv[1];
output = argv[2];
fopen_s(&fin, input, "rb");
if (!fin)
return 1;
fopen_s(&fout, output, "wb");
if (!fout)
return 1;
buffer = new char[BUFFER];
while (!feof(fin)) {
read = fread(buffer, 1, BUFFER, fin);
if (!read)
break;
for (size_t i = 0; i < read; ++i) {
buffer[i] = ~buffer[i];
}
fwrite(buffer, 1, read, fout);
}
fclose(fin);
fclose(fout);
delete [] buffer;
return 0;
}[/code]

will this work?
can u plz help me in understanding this?
i jus need to read n not 2 copy it at some other place.

Comments

  • I edited your post and added code tags, to preserve the indention of the code.

    : #include
    : #define BUFFER 1024
    : //Encrypts a file by flipping all bytes, i.e XOR
    : int main(int argc, char** argv)
    : {
    : char* input, *output;
    : FILE* fin, *fout;
    : void* buffer;
    : size_t read;
    : if (argc != 3)
    : return 1;
    : input = argv[1];
    : output = argv[2];
    : [red]fopen_s(&fin, input, "rb");

    fin is a pointer, there is no reason to pass the address of the pointer itself to the function. Remove the &.[/red]

    : if (!fin)
    : return 1;
    : [red]fopen_s(&fout, output, "wb");

    Same thing here.[/red]

    : if (!fout)
    : return 1;
    : buffer = new char[BUFFER];
    : while (!feof(fin)) {
    : read = fread(buffer, 1, BUFFER, fin);
    : if (!read)
    : break;
    : for (size_t i = 0; i < read; ++i) {
    : buffer[i] = ~buffer[i];
    : }
    : fwrite(buffer, 1, read, fout);
    : }
    : fclose(fin);
    : fclose(fout);
    : delete [] buffer;
    : return 0;
    : }
    :
    : will this work?
    : can u plz help me in understanding this?
    : i jus need to read n not 2 copy it at some other place.
    :
  • thanx...
    i tried to compile the program but it showed following errors:
    line 14:-function 'fopen_s' should have a prototype
    line 26:-size of the type is unknown or zero
    line 26:-not an allowed type

    can u plz help me in removing these errors?
    also i have a doubt:what is the name of the file that we are reading here or where is the name of file specified in this program?
  • : line 14:-function 'fopen_s' should have a prototype

    fopen_s is not a standard C/C++ function, it is a Microsoft-only function that will only work on Microsoft non-standard compilers. The function is a "thread-safe" version of the standard fopen().

    Unless multi-threading is important to your application (in this case it isn't), use the standard C function fopen() instead.


    : line 26:-size of the type is unknown or zero
    : line 26:-not an allowed type

    I think this comes from using size_t. size_t can be found in the standard header stddef.h.
  • thanx...
    the problem of fopen is solved.
    but same problem is repeating in the line:
    buffer[i] =~buffer[i]
    the header file stddef has defined size_t still it is showing error.
    what to do now???

  • You have declared buffer as a "void *" type. So that means that *buffer and buffer[i] are of type "void". The first issue here is that there is no size for type "void" so there is no way for the compiler to access the value at buffer[i] because it can't compute the memory address for that element.

    For this code you need to declare buffer to be a pointer to some type with a known size. Perhaps you intended buffer to be a "char *" type?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories