 Welcome to the course on computer programming. In this session, we are going to look at how to handle text data from files. More importantly, we are going to look at how text data is converted to internal values and how internal values are converted to text data. We saw that normal text input output is handled using standard files sd in and sd out. We know that the standard input output operators C in and C out actually convert data from ASCII characters and to ASCII characters from and to and from internal representation. In this session, we will study another powerful mechanism which handles text input output and performs such conversion. There are special functions available in C plus plus to perform formatted input and output operations. These are called scan f and print f for scan formatted input and print formatted input. Parameters to these functions have a format string which describes how such conversion is to be accomplished. This format string is followed by variables or expressions which are to be read or which are to be printed. What C plus plus does is it applies an appropriate format pattern to each value, either for interpreting characters in the input string and converting these to internal format or for generating output string from the given expression by converting internal values to the output format. Here is the print f function example. Consider, we have a declaration int roll equal to 1, 2, 3, 4, 5 and int batch equal to 1, 1, 2. Obviously, these numbers are stored in variables roll and batch in internal appropriate integer format. If I say print f percent 5D percent 3D batch slash n comma roll comma batch, observe that the first parameter in the print f function is a string. This is called the format string. The other two are names of variables whose values are to be converted into a formatted output. Notice that percent 5D is actually format specified. Percent 3D is another format specified. There is a blank in between here and there is a new line character. This blank and the new line character are produced verbatim on the output. In short, if we use the print f function like this, it will produce this line 1, 2, 3, 4, 5 blank 1, 1, 2 followed by a new line character. Observe that this blank comes from this physical blank. Backslash n also comes from this backslash n in the format string. Percent 5D is used to convert the value of roll into a 5 digit output number. Similarly, percent 3D is used to convert the internal integer batch value 1, 1, 2 into a 3 digit output value. The general format of print f is as follows. It displays one or more values on the terminal. If the value of n, for example, is 523, this print f statement will produce 523 is a number. Observe that in this format string, percent D is the only format specifier. There is no width specified like 3D, 5D. So, it will simply convert n and whatever number of digits are there in n will be printed. So, 523 is printed out. Followed by blank is a number, backslash n. All of these are not format specifiers. So, they are simply reproduced as they are. Of course, there is a new line character which is not shown here. It will take the cursor to the next line. In short, the format string has a format specifier. All other characters are displayed as they are. Now, the specifiers can appear anywhere, but each must correspond to a value appearing after the format string. In this example, there is only one value appearing after the format string. So, there is only one format specifier, but there could be more values here and more format specifier here. These are some examples of format specifiers. Percent 6D will mean 6 digit integer. Percent 7S will mean string which is to be fitted in 7 characters. Percent 8.2F will mean a floating point representation, but total of 8 digits, 2 digits after decimal point. Percent 8.2G means the same thing, but if the number is very large or very small, it will use an E notation if required. Here is an example of scan F. Scan F does the opposite conversion. It uses the format string to interpret values that are given on input and converts each of these values to corresponding internal format. For example, I have M and N as integer variables, X and Y as floating point variables and a string name as a character array. Notice I have written here and M and N and X and Y. The reason is that scan F is trying to get values inserted into these variables. I must obviously pass these variables by pointer reference, otherwise these variables will not change when they come back from the function. It is very important to remember to use and M and N and X and Y whenever we wish to get values for M and X and Y. Why have we not used AND name? Well, the name is actually a character string of 40 characters in array and please note that the name of the array itself is a pointer, so we need not say AND name. Now, if I give input in any one of these lines, it will correctly associate the same value to the variable. So, I can say 25 blank, 2 blanks may be minus 78, 1 blank, 0.00763, 1 blank, 345.29, 1 blank, my name is Chandra. You can see easily what are the values that we intend to give to these variables. Notice that C in could have accomplished the same thing, but scan F does it as per our format specific. Please note that the same format specification will work even if we give data like this. So far, it appears very similar to C in, but there is some special feature scan F which permits us to do things which we cannot do using C in. Consider this example, I have an integer variable A, a floating point variable X and a character array item code. Now, suppose the input data line that we type in or an input data line which is typed or entered into a text 5 contains this 1, 2, 3, 4, 5 fan belt 150.50. Notice there is no blank in between if I use C in to read these values, it will all get confused because it cannot distinguish between three different values. First an integer value, then a character string and then a floating point value. However, using scan F, I can prescribe a format string which says percent 6 D, percent 7 S, percent F. It means first 6 spaces are to be converted into an integer number. Next 7 characters are to be converted into a string and the remaining characters are to be converted into a floating point. Notice the three variables are written as an A, item code and X. As usual, item code being name of the array it itself is a pointer and we are appropriately putting pointer references in scan F so that the values so obtained by the function are actually inserted into our variables in the main program. If I print this in 6 D, 7 S, 6.2 F for example, with backslash t, backslash t which are tab characters and backslash n which is a new line character, this print F function let us see what it will produce. Here is executing that program. So, this is the input line that I had given. Please note that it is a continuous line without any blank and my C in operator would have completely failed to interpret the values correctly. But scan F does it properly and print F produces this output line. There are interesting versions of these functions which are extremely useful when I want to handle text data not from keyboard and I want to produce text output not on monitor but I want let us say to convert a character string which contains such values into internal format or I want to produce an internal character string containing the output converted from some given values. There is a variation of these functions called S print F and S scan F. S print F for example, produces the formatted output for all the expressions as per the format string but instead of producing a line on the output it will actually produce a character string and assign it to S. Obviously S must be declared as a care array with appropriate size. Similarly, if I have a character string in my program in which let us say I have read a line of text from input file somewhere or I have created a formatted text inside S. Then it is possible for me to interpret values inside that character string by using S scan F. Notice that I again give format string and the number of variables for which I wish to interpret the value. In this case the values will not come from STD in but the values will come from a string S. Exactly the same concept can be used for interpreting values or for outputting values not from or into a character string which is inside not from keyboard or monitor but from files. For example, to interpret input from a line to be read from a text file I can actually use F scan F. Notice that F scan F has exactly the same components as scan F namely a format string and a list of variables but the first parameter is F P in which obviously is a file pointer which has been associated with an input text file. In exactly the same passion I can output a formatted line to a text file by giving F P out as the first parameter for F print F. The rest of the specifications for F print F are exactly like print F. In summary we study how to handle formatted text using functions scan F and print F and their different versions. Please refer to C++ tutorials and reference section on the web because this contains complete description of all format specifiers. In particular we note that the scan F and print F has variations called S scan F and S print F for handling input and output to a string and F scan F and F print F for handling input and output from text lines in files. In subsequent sessions we shall use this feature to read large amount of data from a text file and produce special files which are called binary files. Thank you.