Solved

File I/O in C

Posted on 2004-08-01
4
152 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 146
Problem to scan all sheets 3 103
smtp c source code 7 67
Acrinis True image 2 77
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

808 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