Whats Coming?

Chapter 9 - Arrays

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

String Functions and Character Arrays
Because of the way string data types are handled, many of the functions that use character arrays as function arguments were not discussed. Specifically, these functions are gets( ), puts( ), fgets( ), fputs( ), sprintf( ), stpcpy( ), strcat( ), strncmp( ), and strlen( ). Understanding how these functions operate will be much easier now that you are familiar with the concepts of character arrays and null-terminated strings. One of the easiest ways to explain these functions is to show a few program examples.
gets( ), puts( ), fgets( ), fputs( ), and sprintf( )
The following example program demonstrates how you can use gets( ), puts( ), fgets( ), fputs( ), and sprintf( ) to format I/O:
/*
*   strio.c
*   A C program using several string I/O functions
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define iSIZE 20

main( )
{
 char sztest_array[iSIZE];

 fputs(“Please enter the first string  : ”,stdout);
 gets(sztest_array);
 fputs(“The first string entered is    : ”,stdout);
 puts(sztest_array);

 fputs(“Please enter the second string : ”,stdout);
 fgets(sztest_array,iSIZE,stdin);
 fputs(“The second string entered is   : ”,stdout);
 fputs(sztest_array,stdout);

 sprintf(sztest_array,"This was %s a test","just");
 fputs(“sprintf( ) created              : ”,stdout);
 fputs(sztest_array,stdout);

 return(0);
}
Here is the output from the first run of the program:
Please enter the first string  : string one
The first string entered is    : string one
Please enter the second string : string two
The second string entered is   : string two
sprintf( ) created              : This was just a test
Since the strings that were entered were less than the size of sztest_array, the program works fine. However, when you enter a string longer than sztest_array, something similar to the following can occur when the program is run:
Please enter the first string  : one two three four five
The first string entered is    : one two three four five
Please enter the second string : six seven eight nine ten
The second string entered is   : six seven eight ninsprintf( ) created
 : This was just a testPlease enter the first string  : The first
         string entered is   :e ten
The second string entered is   :
Take care when running the program. The gets( ) function receives characters from standard input (stdin, the keyboard by default for most computers) and places them into the array whose name is passed to the function. When you press the enter key to terminate the string, a newline character is transmitted. When the gets( ) function receives this newline character, it changes it into a null character, thereby ensuring that the character array contains a string. No checking occurs to ensure that the array is big enough to hold all the characters entered.
The puts( ) function echoes to the terminal just what was entered with gets( ). It also adds a newline character on the end of the string in the place where the null character appeared. The null character, remember, was automatically inserted into the string by the gets( ) function. Therefore, strings that are properly entered with gets( ) can be displayed with puts( ).
When you use the fgets( ) function, you can guarantee a maximum number of input characters. This function stops reading the designated file stream when one fewer character is read than the second argument specifies. Since sztest_array size is 20, only 19 characters will be read by fgets( ) from stdin. A null character is automatically placed into the string in the last position; and if a newline were entered from the keyboard, it would be retained in the string. (It would appear before the null debug example.) The fgets( ) function does not eliminate the newline character like gets( ) did; it merely adds the null character at the end so that a valid string is stored. In much the same way as gets( ) and puts( ) are symmetrical, so too are fgets( ) and fputs( ). fgets( ) does not eliminate the newline, nor does fputs( ) add one.
To understand how important the newline character is to these functions, look closely at the second run output given. Notice the phrase “sprintf( ) created...”; it follows immediately after the numbers six, seven, eight, and nine that had just been entered. The second input string actually had five more characters than the fgets( ) function read in (one fewer than iSIZE of 19 characters). The others were left in the input buffer. Also dropped was the newline that terminated the input from the keyboard. (It is left in the input stream because it occurs after the 19th character.) Therefore, no newline character was stored in the string. Since fputs( ) does not add 1 back, the next fputs( ) output begins on the line where the previous output ended. Reliance was on the newline character read by fgets( ) and printed by fputs( ) to help control the display formatting.
The function sprintf( ) stands for “string printf( ).” It uses a control string with conversion characters in exactly the same way as does printf( ). The additional feature is that sprintf( ) places the resulting formatted data in a string rather than immediately sending the result to standard output. This can be beneficial if the exact same output must be created twice—for example, when the same string must be output to both the display monitor and the printer.
To review:
  gets( ) converts newline to a null.
  puts( ) converts null to a newline.
  fgets( ) retains newline and appends a null.
  fputs( ) drops the null and does not add a newline; instead, it uses the retained newline (if one was entered).

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

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