Sponsored Content
Top Forums Programming how to write a file to binary format in C ? Post 302334660 by serendipity1276 on Thursday 16th of July 2009 07:00:33 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
p2open(3GEN)					     String Pattern-Matching Library Functions					      p2open(3GEN)

NAME
p2open, p2close - open, close pipes to and from a command SYNOPSIS
cc [ flag ... ] file ... -lgen [ library ... ] #include <libgen.h> int p2open(const char *cmd, FILE *fp[2]); int p2close(FILE *fp[2]); DESCRIPTION
p2open() forks and execs a shell running the command line pointed to by cmd. On return, fp[0] points to a FILE pointer to write the com- mand's standard input and fp[1] points to a FILE pointer to read from the command's standard output. In this way the program has control over the input and output of the command. The function returns 0 if successful; otherwise, it returns -1. p2close() is used to close the file pointers that p2open() opened. It waits for the process to terminate and returns the process status. It returns 0 if successful; otherwise, it returns -1. RETURN VALUES
A common problem is having too few file descriptors. p2close() returns -1 if the two file pointers are not from the same p2open(). EXAMPLES
Example 1: Example of file descriptors. #include <stdio.h> #include <libgen.h> main(argc,argv) int argc; char **argv; { FILE *fp[2]; pid_t pid; char buf[16]; pid=p2open("/usr/bin/cat", fp); if ( pid == -1 ) { fprintf(stderr, "p2open failed "); exit(1); } write(fileno(fp[0]),"This is a test ", 16); if(read(fileno(fp[1]), buf, 16) <=0) fprintf(stderr, "p2open failed "); else write(1, buf, 16); (void)p2close(fp); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
fclose(3C), popen(3C), setbuf(3C), attributes(5) NOTES
Buffered writes on fp[0] can make it appear that the command is not listening. Judiciously placed fflush() calls or unbuffering fp[0] can be a big help; see fclose(3C). Many commands use buffered output when connected to a pipe. That, too, can make it appear as if things are not working. Usage is not the same as for popen(), although it is closely related. SunOS 5.10 29 Dec 1996 p2open(3GEN)
All times are GMT -4. The time now is 06:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy