Jul 24, 2008

Opening Files In Binary Format?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Opening Files In Binary Format?

kvarnerexpress
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:


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.

 

 

 


Reply

switch
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 biggrin.gif

Reply

jibnet
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

Reply

kdr_98
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);


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. c open binary - 3.90 hr back. (2)
  2. how to open binaries in c - 4.11 hr back. (1)
  3. how to get binary format file - 7.21 hr back. (1)
  4. open text file in binary format in c# - 11.79 hr back. (2)
  5. how to open a binary file in text format? - 27.38 hr back. (1)
  6. c# open binany file - 33.72 hr back. (1)
  7. altair binary format - 35.52 hr back. (1)
  8. open file in binary format - 34.96 hr back. (2)
  9. reading binary file in vc - 37.69 hr back. (1)
  10. how to open file in binary - 38.26 hr back. (1)
  11. linux, open a file in binary format - 44.27 hr back. (1)
  12. how to create a file in binary mode java code - 48.88 hr back. (1)
  13. how to open binary format file - 50.17 hr back. (1)
  14. view files in binary format - 50.74 hr back. (1)
Similar Topics

Keywords : opening, files, binary, format

  1. File Format Encyclopedia Release 1.00
    (2)
  2. Binary Tree
    (1)
    Ok my program is supposed to read in from a file and create a linked list of chapters and each
    linked list is supposed to have a link to a binary tree that holds words that are related to each
    chapter so eventually I can code a search function the input file is as follows 3 Drop_Box 10 need
    to drop box with secure box emergency drop urgent Carpet_maintenance 7 grease remove immediately
    cover floor laundry man Smoke_alarm 8 please floor scene urgently scene battery scene box there are
    only 3 lines the top number indicates how many chapters there are and then each line ....
  3. Can Header Files Include Each Other?
    (2)
    Or will it create a circular include problem? What do you do if you need them to, and forward class
    declarations won't work? Forward class declarations only allow you to declare an instance of
    that class, but you can't access member functions, you have to include the header file. So if
    you use MS Visual C++ .NET and the code is placed in the header files, then what is the solution? I
    have two classes(forms) that need to include each other but it's not working. When I try it it
    stops recognizing the classes and gives errors like, "missing storage-class or t....

    1. Looking for opening, files, binary, format

Searching Video's for opening, files, binary, format
advertisement



Opening Files In Binary Format?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE