reading and writing 64bit integer of binary in C

Programming related discussions related to game research
Shokoniraya
Posts: 416
Joined: Sat Sep 15, 2018 5:22 am

reading and writing 64bit integer of binary in C

Post by Shokoniraya »

currently, im working with C (not C++ or C#). it's fast and good
my problem is that how can i read 64_int blocks in C
i can use read(); and write(); of <fcntl.h>, but they can't do the job for 64bit value!

my main question is that is there any io library or header file that i can use it? or i missed something about 64 bit stuffs? (im using 32bit version of tcc)

and one more question, how can i replace a few bytes in binary with c? (or remove some bytes and inject new bytes in middle of file)
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: reading and writing 64bit integer of binary in C

Post by aluigi »

Isn't this enough?

Code: Select all

long long var;
fread(&var, 1, sizeof(var), fd);


Enlarging or shrinking a file by putting/removing something in the middle is not easy, it's ok to do in memory (some work of memmove) and then saving to file.
Shokoniraya
Posts: 416
Joined: Sat Sep 15, 2018 5:22 am

Re: reading and writing 64bit integer of binary in C

Post by Shokoniraya »

fread function with long long var; can read 64 blocks, but not showing 64 integer number in output, is that possible that 32bit compiler can't deal with it?




i attached my code, it will read 16 byte and trying to inject "test.bin" to input file
but it's super slow, and storing a 400 mg file in memory and write it to file with my old pc is really annoying and no make any sense
is there any way to do it in a optimize way? in this case, i mean overwriting fast
like overwritng binary of a file to a specified offset of another file
use: exe_name -e file

slow parts commented with /////START OF SUPER SLOW!!!!!!!!!!! and ends on /////END OF SUPER SLOW!!!!!!!!!!!
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: reading and writing 64bit integer of binary in C

Post by aluigi »

Do you mean not showing to output when using printf?
In that case, yeah printf needs a special type to display decimal 64bit numbers, but they depend by your compiler: %I64d, %lld and so on.
If you need hexadecimal view and want something easy use: printf("%08x%08x". (int)(num64 >> 32), (int)num64);

Regarding the slowness, consider that working on the memory and dumping it to file is faster than shriking and enlarging the file itself because a memmove is thousands times faster than a block-by-block or byte-per-byte operation on file for reserving/removing space + ftruncate if shrinked... it's a mess (indeed quickbms does reimport3 on memory and then back to file if I remember correctly).
Shokoniraya
Posts: 416
Joined: Sat Sep 15, 2018 5:22 am

Re: reading and writing 64bit integer of binary in C

Post by Shokoniraya »

how about this?

Code: Select all

write(target_file, &porting_file, 4096);


i found out that it will dump buffers from memory(not porting_file), if there will be a way to change it, it can be done
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: reading and writing 64bit integer of binary in C

Post by aluigi »

I don't think I understand what you mean.

If your goal is "replacing" some bytes, that's ok since it's just seek+write.

The problem is removing/adding because it requires to move portions of the file.
Shokoniraya
Posts: 416
Joined: Sat Sep 15, 2018 5:22 am

Re: reading and writing 64bit integer of binary in C

Post by Shokoniraya »

i just need a way to fast writing (not byte per byte by script)

Code: Select all

write(target_file, &porting_file, 4096);

this function can read and write as well, but this two thing will explain what i mean

1) write(target_file, &porting_file, 4096);
this command can write, but when i set &porting_file, it will dump real memory to target_file, but it suppose to be porting_file content, not memory ram content, it writes memory ram content to target_file instead of porting_file binary to target_file (maybe &porting_file is wrong, what should i do?)

2) my script can enlarge and shrink file, but main problem is that writing byte per byte is not fast
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: reading and writing 64bit integer of binary in C

Post by aluigi »

Ok so you want to copy the file without copying it whole in memory and without doing it byte-per-byte.
You need to copy it using blocks of memory, for example:

Code: Select all

    static unsigned char    tmp[4096];
    for(len = 0; len < size; len += t) {
        int t = sizeof(tmp);
        if((size - len) < t) t = size - len;
        t = fread(ftmp, 1, t, file_input);
        if(t <= 0) break;
        t = fwrite(tmp, 1, t, file_output);
        if(t <= 0) break;
    }

size is the size of file_input
Shokoniraya
Posts: 416
Joined: Sat Sep 15, 2018 5:22 am

Re: reading and writing 64bit integer of binary in C

Post by Shokoniraya »

thank you, by using with 8192, now it has a worderful speed, 140mb in 3sec!
Shokoniraya
Posts: 416
Joined: Sat Sep 15, 2018 5:22 am

Re: reading and writing 64bit integer of binary in C

Post by Shokoniraya »

thank you sir aluigi, you are great!

by the way, i attached my file, in case if someone need it

note: function name is inject_binary

usage: inject_binary(fd, bnk_name, "test.bin", 4 );
fd= name of your target file [open it with open(); function of fcntl.h (check io.h for more information)]
bnk_name= your target file that you want to do inject or replace in it, same as fd, but you have to use multi-char for it
"test.bin"= name of the file that will inject or replace to your target file
4= you can put any size that fat32 supports it, 0 size means inject, and none-zero size is for selecting specified bye (in this example, it's 4), and replace your target 4 byte with your "test.bin"

important note: injecting/replacing offset is current location of fd