Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 . Purpose In this programming assignment you will design and implement your own core input and output functions of the C / C +

1. Purpose
In this programming assignment you will design and implement your own core input and output
functions of the C/C++ standard I/O library: stdio.h.
2. Linux I/O
Unix provide system calls for file I/O such as open(), read(), write(), and lseek(). However, these system
calls are not supported on non-Unix based systems such as Window. Therefore, the C/C++ standard I/O
library was created so that applications which require these functions can be easily ported through
recompilation across these systems.
3. C/C++ Standard I/O Library Overview
The standard I/O library is an architecture independent library that allows C/C++ programs to read and
write files instead of directly calling the underlying OS system calls. The library's functions add buffering
and provide a user-friendly file stream interface (FILE *). The file stream interface is implemented by
the standard I/O library utilizing the underlying system calls. When reading and/or writing small byte-
counts (e.g., reading one line at a time from a file), buffered functions are faster.
The read() and write() system calls operate on file descriptors and read/write from/to buffers not
strings. A file descriptor is just an integer referring to a currently open file. The OS uses that number as
an index into the file descriptor table of files currently in use to access the actual device (e.g., disk,
network, terminal).
On the other hand, file streams operators (fread/fwrite) interact directly with file streams: FILE *. File
streams are dynamically allocated and allow reading/writing raw data. File stream operators, fread()
and fwrite(), use the type void * since there are no data-specific requirements.
The core input and output functions defined in include:
Function name Description
fopen opens a file
fflush synchronizes an output stream with the actual file
setbuf, setvbuf sets the size of an input/output stream buffer
fpurge clears an input/output stream buffer
fread reads from a file
fwrite writes to a file
fgetc reads a character from a file stream
fputc writes a character to a file stream
fgets reads a character string from a file stream
fputs writes a character string to a file stream
fseek moves the file position to a specific location in a file
feof checks for the end-of-file
fclose closes a file
printf prints formatted output to stdout
Figure 1 below shows how the standard I/O library utilizes a buffer to reduce the number of read and
write system calls with filestreams.
Figure 1. This figure shows Standard I/O functions using FILE instead of file descriptor.
4. FILE Data Structure and fopen()
Upon a file open, fopen() returns a pointer to a FILE object that maintains the attributes of the opened
file.
On canvas is posted a version of the header file we will use for the project. The following shows the
class FILE definition:
#ifndef _MY_STDIO_H_
#define _MY_STDIO_H_
#define BUFSIZ 8192// default buffer size
#define _IONBF 0// unbuffered
#define _IOLBF 1// line buffered. Do not need to implement this mode.
#define _IOFBF 2// fully buffered
#define EOF -1// end of file
class FILE
{
public:
FILE() :
fd(0), pos(0), buffer((char *)0), size(0, actual_size(0),
mode(_IONBF), flag(0), bufown(false), lastop(0), eof(false){}
int fd; // a Unix file descriptor of an opened file
int pos; // the current file position in the buffer
char *buffer; // an input or output file stream buffer
int size; // the buffer size
int actual_size; // actual buffer size when read() returns # bytes smaller than size
int mode; //_IONBF, _IOLBF, _IOFBF. You do not need to implement _IOLBF.
int flag; // O_RDONLY
// O_RDWR
// O_WRONLY | O_CREAT | O_TRUNC
// O_WRONLY | O_CREAT | O_APPEND
// O_RDWR | O_CREAT | O_TRUNC
// O_RDWR | O_CREAT | O_APPEND
bool bufown; // true if allocated by stdio.h or false by a user
char lastop; //'r' or 'w'
bool eof; // true if EOF is reached
};
#include "stdio.cpp"
#endif
When opening a file, the fopen() function receives not only the file name to open but also various file
access modes:
r Open text file for reading.
r+ Open for reading and writing.
w Truncate file to zero length or create text file for writing.
w+ Open for reading and writing. The file is created if it does not exist, otherwise truncated.
a Open for appending (writing at end of file). The file is created if it does not exist.
a+ Open for reading and appending (writing at end of file). The file is created if it does not
exist. The initial file position for reading is at the beginning of the file, but output is always appended to
the end of the file.
The fopen() function must:
instantiate a FILE object
initialize it according to the file modes
allocate a file stream buffer within the FILE object
open a file using the corresponding OS system call, (e.g., open in Unix)
Binary vs. text files: Linux doesn't differentiate between text and binary files, so 'b' type for fopen() has

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

5. What is the purpose of the report?

Answered: 1 week ago

Question

3. Outline the four major approaches to informative speeches

Answered: 1 week ago

Question

4. Employ strategies to make your audience hungry for information

Answered: 1 week ago