Copy contain of an unicode file in to the char array in c

BattleTested_закалённый в бою

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.

Alan Stokes

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to copy a .txt file to a char array in c++

From Dev

How to copy a char pointer into a char array?

From Dev

How to copy a sentence into a char array

From Dev

Decimal to Unicode Char in C++

From Dev

copy a char array to another char array of arrays

From Dev

Copy struct into char array

From Dev

How to copy array char into string or int* (void *) in c++?

From Dev

Function to copy names from a file into a char * array

From Dev

How to copy char* to char* array in C

From Dev

BMP Char Array to file c/c++

From Dev

gzip and copy paste content to a String/ char array in C program

From Dev

How to copy uint8_t array in char array in c

From Dev

How to copy a .txt file to a char array in c++

From Dev

C++ copy #define to char array

From Dev

c++ char array unicode cyrillic symbols incorrect

From Dev

File to char and char to array C

From Dev

Copy char input into array of strings

From Dev

How to copy char* to char* array in C

From Dev

copy data from txt file to char pointer in c++

From Dev

Read a file char by char and store data to a bidimentional array C

From Dev

BMP Char Array to file c/c++

From Dev

Reading binary file into char array in c++

From Dev

gzip and copy paste content to a String/ char array in C program

From Dev

How to copy uint8_t array in char array in c

From Dev

how can I make a copy the same value on char pointer(its point at) from char array in C?

From Dev

C - Reading CSV file in char array

From Dev

Copy file with FOR /F %I IN () DO (copy) command, file contain space

From Dev

Copy text file into char* array

From Dev

Objective C - unicode (Urdu) file copy

Related Related

HotTag

Archive