Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C I/O for C++ Programmers: Understanding the Basics of C I/O Functions in EECS 678 - Prof., Study notes of Operating Systems

An introduction to c i/o functions for students enrolled in eecs 678: introduction to operating systems. Many projects in this course involve c i/o functions, which are essential for understanding past and present c/c++ code and features of newer languages like python and php. The tutorial covers string output using printf, sprintf, and fprintf, as well as file handling using file descriptors and file pointers.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-9fs-2
koofers-user-9fs-2 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download C I/O for C++ Programmers: Understanding the Basics of C I/O Functions in EECS 678 - Prof. and more Study notes Operating Systems in PDF only on Docsity! EECS 678: Introduction to Operating Systems Tutorial: C I/O for C++ Programmers Many students reach EECS 678 without encountering C I/O output functions; instead, C++ stream-oriented I/O is the familiar I/O model for use with the console and files. EECS 678 projects (e.g. the IPC puzzle and NACHOS projects) will include example or stubbed code containing many uses of C I/O. This tutorial is intended to introduce students to the C I/O functions in order to mitigate that unfamiliarity’s role in the course projects. Perhaps even better reasons to encourage understanding of C I/O is its heavy usage in past and even present C/C++ code as well as its influence on features of new languages including Python and PHP. Prelude: Strings When the C I/O functions were developed, there existed no standard string object other than the character array. Hence, most I/O functions accept a char *buffer and a int size parameter. Students bridging this gap may wish to review the C String functions (e.g. strcpy, strcat, etc.). Part I: String Output (printf and friends) The grunts of C I/O are the formatted-printing functions printf, sprintf, and fprintf. int printf( const char *fmt, ... ); printf exemplifies the basic concept embodied by this family of functions. The input param- eter fmt is a NULL-terminated string. After a transformation, this string is printed to stdout (analogous to cout). The steps of the transformation are determined by the format string in a precise way. When certain characters are encountered in the format string, they are replaced by a string representation of their corresponding argument in the variable argument list. For instance, char *szName = "Nick"; printf( "Hello \%s!" , szName ); would print ”Hello Nick!” to stdout. The %s in the format string is called a type specifier. There are many type specifiers (e.g. %d for integers), see the table at the end of this Part or check on the Internet–just search for ”printf type specifiers” and a full table will be a click or two away. int sprintf( char *buffer, const char *fmt, ... ); int fprintf( FILE *file, const char *fmt, ... ); sprintf and fprintf behave just like printf except that sprintf outputs to a string and fprintf outputs to a file pointer (which is discussed below). With sprintf, be sure the final string will fit in the specified buffer. The FILE *file parameter of fprintf is discussed in the next section. 1 scanf and friends do for input what printf and friends do for output. The scan functions can be used to parse strings, i.e. ”Nick ran 2.5.” could be used to set the values of a string variable for the name and a distance variable for the number of miles. Look at the scanf man page (”man 3 scanf”) or online for more information. %% Literal percent sign, i.e. ’%’ %s char* string pointer, e.g. argv[0] %c Character %d Integer %u Unsigned integer %f Floating-pointer number %x Hexidecmal representation of a number Table 1: Common Format String Type Specifiers Part II: File Handling File descriptors are the mechanism by which processes identify character streams (files, sockets, etc.) when cooperating with the operating system. The system calls open, close, read, and write work with file descriptors. As the lowest-level mechanism, file descriptors are not the most convenient file abstraction with which to program. The FILE *file argument in the prototype of fprintf above is called a file pointer and is the most common element of file-based I/O in C. It is the C Standard Library’s abstraction built on top of file descriptors. Functions such as fopen, fclose, fgets, or fputs create or target file pointers. There are three global file pointers stdin, stdout, and stderr which correspond to cin, cout, and cerr, respectively, from C++. Again, extensive reference material is available. The most significant difference between file descriptors and file pointers is that file pointers are buffered and file descriptors are not. Consider a program that prompts the user for input. If that program is written with file descriptors and file descriptors system calls, there is no buffering and writeing to the console before reading from it will certainly show the user the prompt string before expecting a response. If the program uses file pointers instead, I/O buffering may cause a delay in the presentation of the prompt to the user. It might expect a response before the user sees the request to type! int fflush( FILE *file ); This kind of situation can be avoided by use of fflush; it forces a buffer to be written, and it can help in situations where buffering could cause confusion. In the interactive program example, the code would write to the screen via the file pointer and flush the file pointer before reading input to be certain that the user sees the prompt before needing to type a response. These file pointer functions are discussed in the following paragraphs: FILE *fopen( const char *filename, const char *mode ); 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved