how to write a file to binary format in C ?


 
Thread Tools Search this Thread
Top Forums Programming how to write a file to binary format in C ?
# 1  
Old 07-16-2009
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.
# 2  
Old 07-16-2009
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.
# 3  
Old 07-16-2009
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.
# 4  
Old 07-17-2009
Quote:
Originally Posted by serendipity1276
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?
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Binary write POSIX-ly.

Hi guys and gals... I am now beyond the limits of my POSIX knowledge here. Below is a piece of code that runs perfectly well on small string lengths, BYTE sizes up to around 1KB, (3KB of octal text). It generates byte vlues from 0x00 to 0xFF. It passes Shell Check's requirements and... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. Shell Programming and Scripting

Convert jpg file to binary format

Hi Team, Here's the requirement. I have a image file in jpg format in unix. Now I need to i. convert the jpg format to binary format ii. followed by loading the binary file to Oracle db. Can anyone help me out? Thanks Krishnakanth Manivannan (4 Replies)
Discussion started by: kmanivan82
4 Replies

3. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

6. UNIX for Dummies Questions & Answers

Converting binary file to readable format in Ksh

In Unix/Ksh, when I try to look inside a file it says that the file may be a binary file and if I want to see it anyway. When i say 'yes', it shows me the content filled with unreadable symbols (looks like binary). Is there a command that I can run from the Unix prompt to convert/translate that... (3 Replies)
Discussion started by: arthurs
3 Replies

7. Shell Programming and Scripting

Is there any script which convert binary file to CSV format

Dear guys; I have a binary file and I need to convert its data to csv format ...appreciating your help. Best Regards (14 Replies)
Discussion started by: ahmad.diab
14 Replies

8. Programming

Reading a binary file in text or ASCII format

Hi All, Please suggest me how to read a binary file in text or ASCII format. thanks Nagendra (3 Replies)
Discussion started by: Nagendra
3 Replies

9. UNIX for Dummies Questions & Answers

write data into a text file in bold format

Hi, can anyone help to write data into a text file in bold format and rollback to actual format. Thanks, Regards, Milton Y. (1 Reply)
Discussion started by: miltony
1 Replies

10. Programming

Binary to text format conversion

Hi, Please can any one tell me how to convert binary data to text format and vice versa. If possible give me the algorithm or C program. Thanks in advance Waiting for reply Bye:o (5 Replies)
Discussion started by: manjunath
5 Replies
Login or Register to Ask a Question