Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Opening Files In Binary Format?

Recommended Posts

i'm trying to iterate through a file and copy its character by character into a new exe file so i can later adapt this functionality into a program that i've already written in two or three other languages. its just to dip my fingers back in C since i've gotten rusty from since the last time i used to harrass this forum, lol. i seem to believe that once i use fopen in read mode that it opens the file in binary mode but it still dosen't give me the results i want, watch a small sample of the code please.


Code:


int main(void){ int a; FILE * FROM = fopen("C:\\test.exe","r"); FILE * TO = fopen("C:\\result.exe","w");   while((a = fgetc(FROM))!= EOF) {  fputc(a,TO);             } ........ // close files etc}

i get an error saying result.exe is not a valid application
i've already written this program in python, java, and even C# if i remember correctly but i thought i had already solved this problem in C, maybe i forgot how. anyway thanks to those of you who are going to help.

Share this post


Link to post
Share on other sites

according to the MSDN library (search for "fopen()" ), you need put 'b' in front of the 'w' and 'r' to specify binary mode. also you may want to try substituting 'w' and 'r' for 'w+' and 'r+' respectively. They essentially won't make any difference to the program for what you are doing.Take a look at the MSDN library. It's got a VERY comprehensive coverage of this function, and the whole C Runtime Library for that matter.Cheers :)

Share this post


Link to post
Share on other sites

Hey, i was just doing some research on opening files in binary mode and i found that if we open a file like DOS in C in binary mode.. and then search for an internet command with fseek() function and then replace the command with the fwrite() function then the command gets changed.. i tried it and it worked.For any info please mail at jibranbhat@gmail.com

Share this post


Link to post
Share on other sites

The problem is that you are getting chars from the file.The datatype char uses only 7 bits from the 8 (the MSB is used as sign bit).You can solve your problem by geeting integer values with the commands fscanf() and fprintf().fscanf(FROM,"%d", a);fprintf(TO, "%d", a);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.