i write a c code as following, that copy a file. it works truely for unicode
files (exe, rar for example), i use of a char
data-type array to copy file "block" in that. i know that, char data-type
just can store 1
byte as extended ASCII
standard.
in fread()
function, used of buffer[buflen]
variable as char
array due to copy a block of an exe
file (100
byte) in that, then copy buffer[buflen]
contain in an other file. how it possible that, a block of unicode
characters, stores in char
? why this code works for unicode
files truely without any problem?
copyFile
function :
void copyFile(const char *src, const char *dst)
{
const int buflen = 100;
char buffer[buflen];
long fileSize, curFileSize, offset = 0;
FILE *r, *w;
r = fopen(src, "r+b");
w = fopen(dst, "w+b");
fseek(r, 0, SEEK_END);
fileSize = ftell(r);
fseek(r, 0, SEEK_SET);
while(fileSize - (curFileSize = ftell(r)) >= buflen)
{
fseek(r, offset * buflen, SEEK_SET);
fread(&buffer, sizeof(buffer), 1, r);
fwrite(&buffer, sizeof(buffer), 1, w);
offset++;
}
if ((fileSize - curFileSize) != 0)
{
fseek(r, (offset - 1) + (curFileSize), SEEK_SET);
fread(&buffer, fileSize - curFileSize, 1, r);
fwrite(&buffer, fileSize - curFileSize, 1, w);
}
fclose(w);
fclose(r);
}
entrypoint
section :
int main()
{
copyFile("e:/1.exe", "e:/2.exe");
return 0;
}
what is the reason of using char
data-type
or a struct
(containing of char
) in fread
and fwrite
functions?
Thanks of everybody to help me.
Any file, regardless of encoding, is just a sequence of bytes. The char
type can store any byte, so you're just copying the file byte for byte. (char
is used in C and C++ as both a character type and a numeric type capable of holding a byte. This can be confusing, but both usages are valid.)
fread
and fwrite
are specified in terms of char
because they read and write bytes.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments