-1

So, I have a C program where I am trying to save the binary code from a file that the user specifies. For example, if a user inputs a .txt or a .jpeg, or a .pdf, or whatever file extension they have, then I will be able to gather the binary code of the file. The catch is that the file will not be exclusively a .bin.

This is very specific to a project I am trying to make, so I don't see any documentation on how to achieve this. I believe this is achievable because technically every file is in binary, so, especially in C, this should be possible.

So far I have experimented by using fopen with an rb argument: file = fopen(filename, "rb"); because I am trying to modify the binary specifically. And i'm trying to gather the binary by using fseek(file, 0, SEEK_END); which should traverse the file.

I am coming from a Python/Java background, so any guidance or help is appreciated.

My question is what is the best way to achieve this?

2

1 Answer 1

1

You are trying to draw a distinction where there isn't any- all files are binary. Using fopen is the right approach- this opens the file for reading/writing by creating a file descriptor. fseek, however, is not what you are looking for- this moves the file pointer without actually doing anything (ie it doesn't read the file). What I think you are actually looking for is fread and fwrite, which actually manipulate the file binary.

You should read more about how file IO is done in C, and come back if you have specific questions about how to do this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.