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
7 replies to this topic
#1
Posted 09 April 2010 - 06:36 AM
|
|
|
#2
Posted 09 April 2010 - 07:09 PM
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
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)
Download the new operating system programming kit! (some assembly required)
#3
Posted 10 April 2010 - 05:42 AM
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!
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
Posted 10 April 2010 - 05:48 AM
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
Posted 10 April 2010 - 08:59 AM
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
Posted 10 April 2010 - 09:44 AM
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
Posted 10 April 2010 - 10:16 AM
Thank you, it's working now :)
#8
Posted 10 April 2010 - 03:39 PM
Don't use that code without adding error checking.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









