#Learning to write .bin files.

1 messages · Page 1 of 1 (latest)

onyx topazBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

late fossil
#

what isn't working about your implementation for 4?

#

what do you mean by 'removing bytes'?

#

'removing bytes' from an array doesn't really make sense in the C world

#

an array is a chunk of bytes with a fixed size

#

but what do you mean by 'remove anything', as in make all of the bytes 0?

#

That isn't possible, an array has a fixed size once it's created

dense jackal
#

Maybe I should try a different approach.

late fossil
#

Not really, because you don't know the sizes at compile time you're creating VLAs

#

Which are somewhat of a sin in C

#

I would recommend using dynamic memory allocation (e.g. malloc)

dense jackal
#
+ 1. Creates x random bytes and assign it to a variable {somehow}...
+ 2. Save the contents (of the variable) to a .bin file.
+ 3. Read the contents of a .bin file and store it to a variable {somehow}...
+ 4. Copy the last x bytes of the {somehow created} variable and assign it to another variable. Delete the copied bytes from the {somehow created} variable...
#

@late fossil how would you do this?

#

*random bytes

late fossil
#

Malloc, realloc, etc.

#

Just move from arrays to dynamically allocated stuff

dense jackal
#

I see. I used arrays because I recently learned about them, however malloc is new to me.

late fossil
#

Its a learning curve, but worth it :(

#

:)*

dense jackal
#

I can already sense the memory leaks.

#

What are VLAs?

#

@late fossil

#

Ah, I think I can also just move the file pointer:

fseek(file, -number_of_bytes_to_read, SEEK_END);

late fossil
#

VLAs are variable length arrays

#

arrays get allocated on the stack, and usually the compiler knows the size of everything on the stack at compile time

#

but it doesn't know that about VLAs, which means that they're really slow to use

dense jackal
#

Ah good to know 🙂 Thanks

onyx topazBOT
#

@dense jackal Has your question been resolved? If so, run !solved :)

#

@dense jackal

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

dense jackal
#

!solved