Jump to content

fopen with binary mood

- - - - -

  • Please log in to reply
7 replies to this topic

#1
peter576

peter576

    Newbie

  • Members
  • Pip
  • 4 posts
Hi everyone!
I have a little program with fopen. In normal mood it's working, but when I want to write something to a file in binary mood it freezes. I guess "fputc" is not the right function here, but I have no other idea. Can someone tell me how can I write something to a binary file? A bit with the value 1 for example.

Thank you!
Peter

#2
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Okay first of all it's "mode" not "mood". Also, binary mode doesn't mean you are writing 1s and 0s. It's just like normal mode, except normal mode will filter some bytes in certain cases. fputc should not freeze the program. If that happens, there is probably something wrong with your compiler or operating system.
I have a tutorial on reading and writing files you can take a look at if you are having trouble:
http://forum.codecal...ng-files-c.html
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#3
peter576

peter576

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you for your help!
Now it's ... not freezing which is strange because I've already read a similar tutorial and binary mode didn't work, but I,m happy that it's working after all. Your tutorial is great!

Now that I know that binary mode is different what I meant, how can I write whatever to my file what I need, I mean, I've tried it with 'a', it worked, and I also tried it with integer with the value 92, and it didn't do anything, but it's running. Do you have any idea how could I write 1s and 0s to my file?

Thank you!

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
If you want to see 1s and 0s in the file then you do NOT want binary mode. What you have to do is first convert an integer to a binary string then write that string to the file (open the file in text mode, not binary mode). This link will show you how to convert decimal to binary.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
peter576

peter576

    Newbie

  • Members
  • Pip
  • 4 posts
I don't want to write to see 1s and 0s. I want to be able to write to the file every ascii character, for example #11 or #234, #13 and so on. So it won't be a normal text in the end...

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Ok, here is how to write an integer in binary mode
FILE* fp = fopen("file.dat", "wb");
int x = 123;
fwrite( (char *)&x, 1, sizeof(int), fp);
fclose(fp);

Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
peter576

peter576

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you, it's working now :)

#8
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Don't use that code without adding error checking.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users