Solved

File I/O in C

Posted on 2004-08-01
4
150 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
Comment Utility
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
Comment Utility
> 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now