how to write a file to binary format in C ?

I'm in the Solaris environment. I want to write data to a file, but I don't want it to be easily read from the C shell. For example, here's my code:

main ()

{
FILE *fo;

fo = fopen ("filename", "w");
fprintf (fo, "This is a test.\n");
fclose (fo);
}

Anyone can open up that file "filename" and they can see the content of that file being:

This is a test.

How do I write this information so that it's not easily readable from the C shell, let's say write it to a binary format? And then, how will I be able to read that binary file back and convert it into an ASCII file? Thanks.

You need fwrite that does block I/O...this way no one can read the file as written and it can only be read fread.

binary and text files ideas are really baggage from windows. The unix file system does not know about binary and text. Simply using fopen("filename", "wb") (open for binary writing) will not make the text unreadable. If you are doing this to hide passwords - don't ever try to write security stuff to a file without real encryption.

Bad idea.

If the information is sensitive, try crpyt. You edit a file then use crpyt to encrypt it.
crypt also decrypts the data. While crypt is not perfect protection it is okay inside the confines of a secure system.

If you are sending this data out on the internet try GNU gpg.

If you are just playing, look into XOR "encryption"
XOR cipher - Wikipedia, the free encyclopedia
The XOR bitwise operator ^ in C can do this for you.

It already is binary. There is no special "data only readable by C programs" format. If you don't want readable text, don't write readable text.