?
Solved

File I/O in C

Posted on 2004-08-01
4
Medium Priority
?
156 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 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.
Suggested Courses

764 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