Solved

File I/O in C

Posted on 2004-08-01
4
154 Views
Last Modified: 2010-04-15
How can we output fixed no. of bytes to a file in C ? e.g 'long int' datatype is 8 bytes, how can we output only 6 bytes to a  file ?
What are the formatted File I/O functions avaialble when programming on UNIX in C ?
0
Comment
Question by:sachinjamdhade
4 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 11689176
FILE *f;
f = fopen("myFile","w");  // Open the file for writing
fwrite(someBuff,1,6,f);    // write the first 6 bytes pointed to by someBuff
fclose(f);

Some File I/O function to look at are:
fopen()
fclose()
fread()
fwrite()
fprintf()

These function are available on all platforms as well.
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 125 total points
ID: 11689266
> e.g 'long int' datatype is 8 bytes, ...
Not on all platforms.  On most 32-bit systems, long int is 4 bytes. But let's continue...

> e.g 'long int' datatype is 8 bytes, how can we output only 6 bytes to a  file ?

The first question to ask is "Which six bytes (of the eight) do you want to write out?".  
For the purpose of this discussion, I will assume you want to write out the low-order
6 bytes (the least significant 48-bits).  Before you dive into how to address individual
bytes in a long integer, you will need to understand byte-order differences for different
computer architectures:  http://www.netrino.com/Publications/Glossary/Endianness.html

Once you understand that, you can cast the long integer to an array of bytes and write
out a subset of the bytes:

int64_t num;            // our 64-bit integer
char * bytes = (char *)#      // bytes points at num, but thinks it's an array of 8 bytes
int fd;      // an open file descriptor

#if defined(LITTLE_ENDIAN)
      write(fd, bytes, 6);
#elif defined(BIG_ENDIAN)
      write(fd, bytes+2, 6);
#endif

It must be read back on the same ENDIAN-ness platform in the same way:
num = 0;      // make sure the high two bytes are zero'ed out first
#if defined(LITTLE_ENDIAN)
      read(fd, bytes, 6);
#elif defined(BIG_ENDIAN)
      read(fd, bytes+2, 6);
#endif


> What are the formatted File I/O functions avaialble when programming on UNIX in C ?

printf() [which writes to stdout] and its derivatives fprintf() [which writes to an opened file]
and sprintf() which writes into a buffer.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cURL: stopping a http transaction before it's finished 3 131
SQL handling single and double quotes 3 99
Socket Programming (Unix) 8 153
An API detour question 7 103
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question