|
|
|
|
|
Standard Input and Output in CCommand line and file handling in a library tutorial.
In this C library tutorial, we explore stdio, standard input and output in C, both for command line input and output and file handling.
IntroductionThis C programming tutorial deals with the use of standard input and output functions under C. Unlike other languages, such as BASIC, there are no built-in functions for handling I/O, but the ANSI compliant stdio library exists to provide functions to handle input and output. Before you begin, it might be worthwhile to take a quick look at the C Header File tutorial to refresh your memory regarding the use of C header files and libraries. Also of note might be the General Programming series, for those not totally familiar with programming concepts in general. The printf and scanf functionsThe two functions printf and scanf handle I/O in much the same way. They each take a formatting string and a set of parameters that either deliver or receive data. The latter are passed by reference, since they will be modified during the function call. Typically, the programmer will make use of the following formatting sequences when printing formatted output, or receiving input: %d, %ld : Integers %f, %lf : Floating point numbers %s : Strings (char * or char []) %c : Characters \n, \t : Control sequences linefeed and tab In addition, these formatting sequences can be combined with precision and width specifiers. For example: %.02f i.e. 0.04 or 42.98 These formatting strings are passed to the printf or scanf functions as constants: printf ( "%s\n", "Hello World" ); scanf ( "%s\n", szInputString ); In the first case, the function call will output the string (in this case, a constant) and in the second a string will be read in. The same approach can be applied to all data types supported by the formatting strings. The key to proficiency, as with many areas of C programming, is in practice and experimentation. From printf and scanf to fprintf, fscanf, sprintf and sscanfOne of the interesting aspects of the stdio library is that the same formatting strings can be used with console I/O as well as files and even strings. This last might require some explanation. In essence, the function call: sprintf ( szTarget, "%s %03d", "Number", 42 ); Will place the string: Number 042 Into the szTarget variable. Omitting the variable name, and calling the printf function will output the string to the screen. The same trick can be played with the sscanf function, which will parse a string into a set of variables. The value returned by this function indicates the number of fields that were converted. The fprintf and fscanf functions provide the same interface, but applied to files as opposed to strings. It is worth mentioning that a file is opened with the following function call: fopen ( char * szName, char * szOperations ); The szOperations argument details the operations that the program shall be performing on the file. For example, "w" will open the file for writing - if it does not exist it will be created, if it does it will be overwritten. To avoid this, open the file as "a" to append data, or "rw" to read and write. Other possibilities exist, but that is left to reader experimentation. The function returns a pointer to a file (FILE *) which can be passed to fprintf, fscanf, or fread/fwrite (below). It can also be passed to the fclose function which will close the file. File handling with fwrite and freadBinary data is best handled with the fread and fwrite functions: fread ( void * Buffer, int nSize, int nCount, FILE * fHandle ); fwrite ( void * Buffer, int nSize, int nCount, FILE * fHandle ); In both cases, the fHandle should point to a file opened with a call to fopen, nSize refers to the size of the object to be written, and nCount to the number of items to be written. The Buffer contains the data source (or target). The advantage of using fwrite and fread is that any data type can be used - be it a built in data type (remember to always use the sizeof function to determine the size of each element), or a user defined structure. The formatted I/O functions can not deal with user defined functions, making it necessary to read/write each component separately. For more detailed discussions of file handling, please see the C Tutorial for File Handling Commands.
The copyright of the article Standard Input and Output in C in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Standard Input and Output in C in print or online must be granted by the author in writing.
|
|
|
|