File and Data Access


Every program that runs from the UNIX shell opens three standard files. These files have integer file descriptors and provide the primary means of communication between the programs; they also exist for as long as the process runs. You associate other file descriptors with files and devices by using the open system call.(See Table 9.14.)

Table 9.14: UNIX Standard File Descriptors

File

File Descriptor

Description

Standard input

Standard input file provides a way to send data to a process. By default, the standard input is read fromthe keyboard.

Standard output

1

Standard output file provides a means for the programto output data. By default, the standard output goes to the display.

Standard error

2

Standard error is where the program reports any errors occurred during program execution. By default, the standard error goes to the display.

In Windows , when a program begins execution, the startup code automatically opens three streams:

  • Standard input (pointed to by stdin )

  • Standard output (pointed to by stdout )

  • Standard error (pointed to by stderr )

These streams are directed to the console (keyboard and screen) by default. Use freopen to redirect stdin , stdout , or stderr to a disk file or a device.

The stdout and stderr streams are flushed whenever they are full, or, if you are writing to a character device, after each library call. If a program terminates abnormally, output buffers may not be flushed, resulting in loss of data. Use fflush or _flushall to ensure that the buffer associated with a specified file or all open buffers are flushed to the operating system, which can cache data before writingit to disk. The commit-to-disk feature ensures that the flushed buffer contents are not lost in the event of a system failure.

Low-Level File Access

The low-level I/O functions invoke the operating system directly for lower-level operation than that provided by standard (or stream) I/O. Function calls relatingto low-level input and output do not buffer or format data.

Low-level I/O functions can access the standard streams opened at program startup using the standard file descriptors. They deal with bytes of information, which means that you are using binary files, not text files. Instead of file pointers, you use low-level file handles or file descriptors, which give a unique integer number to identify each file.

UNIX Example: Writing to Standard Output

The following sample writes to the standard output file descriptor, and if any errors occur, it writes an error message to the standard error file descriptor.

 #include <unistd.h> int main() {     if ((write(1, "Here is some data\n", 18)) != 18)         write(2, "A write error has occurred on file descriptor 1\n",46);     exit(0); } 

Win32 Example: Writing to Standard Output

The following sample writes to the standard output file descriptor, and if any errors occur, it writes an error message to the standard error file descriptor.

Note  

Just as in UNIX, the Win32 cmd.exe shell redirects standard error from the command line with the 2> operator (without the quotes).

 #include <io.h> void main() {     if (write(1, "Here is some data\n", 18) != 18)         write(2, "A write error has occurred on file descriptor 1\n", 46); } 

UNIX Example 1: Using Standard Input and Standard Output

The following sample reads characters from the standard input file descriptor and writes that information to the standard output file descriptor. If any input/output errors occur, an error message is output to the standard error file descriptor.

 #include <unistd.h> int main() {     char buffer[129];     int num_read;     num_read = read(0, buffer, 128);     if (num_read == -1)2         write(2, "A read error has occurred\n", 26);     if ((write(1,buffer,num_read)) != num_read)         write(2, "A write error has occurred\n",27);     exit(0); } 

Win32 Example 1: Using Standard Input and Standard Output

The following sample reads characters from the standard input file descriptor and writes that information to the standard output file descriptor. If any input/output errors occur, an error message is output to the standard error file descriptor.

 #include <io.h> void main() {     char buffer[129];     int num_read;     num_read = read(0, buffer, 128);     if (num_read == -1)         write(2, "A read error has occurred\n", 26);     if ((write(1, buffer, num_read)) != num_read)         write(2, "A write error has occurred\n", 27); } 

UNIX Example 2: Using Standard Input and Standard Output

The following sample reads up to 1 KB of characters from the input file and writes that information to the output file. If any input/output errors occur, an error message is output to the standard error file descriptor for any input or output errors.

 #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int main() {     char block[1024];      int in, out;      int num_read;     in = open("input_file", O_RDONLY);      if (in == -1) {         write(2, "An error has occurred opening the file: 


UNIX Application Migration Guide
Unix Application Migration Guide (Patterns & Practices)
ISBN: 0735618380
EAN: 2147483647
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net