Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File I/O in C

Posted on 2004-08-01
4
Medium Priority
?
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

604 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