fprintf() function comes in very handy in many situations, two of which i come across are:
1) Keeping track of which login ids from the given login ids were able to login to application.
2) If the script fails during run time and we want to record the group name and Vuserid for finding the root cause after the execution.
This function writes a fomatted output to stream
Declaration - int fprintf( FILE *fp, const char *fs [,arguments ] );
fp = Pointer to a file (actually stream)
fs = Formated string we want to write to the file (actually stream)
Return Value -
On sucess - Total number of characters printed.
On error - Negative number
fs or the formated string can be formated using different format tags prototype of which is %[flags][width][.precision][length]specifier
Most common specifiers are following:
%d = Displays and integer
%f = Displays a floating-point number in fixed decimal format
%e = Displays a floating-point number in exponential notation
%s = Displays string of characters
%u = Displays unsigned decimal integer
%c = Displays a character
Since most of the format tags will never be used and there are lots I am just giving one or two examples for all the tags below.
Flags = flags can be used for various purposes. Main is to format data as per requirment. For example -
1) - = is used to left justify the field.
2) 0 = to pad field with zeros rather than blanks.
Width = Every data format is provided with minimum required width to hold the same. Width format tag can be used to increase it further.
1) %[width]d - will increase the field width of given integer.
2) %[width]s - will increase the width of given string.
Precision = This format tag takes different meanings for different format types.
1) %[total].[decimal]f - Here total field length will be [total] and [decimal] of these will hold the decimal part for a float value.
2) %[minimum].[maximum]s - Here minimum width is [minimum] and maximum width is [maximum] for a string value. If string is more than [maximum]
value it will be cropped to [maximum] value.
Length = length modifier is used to let printf know that we want to print very big or very small variables. For example -
1) short int - Modifier to be uses in this case is 'h'. Short int is always takes less or equal bits as int. that is short int <= int <= long.
2) long double - Modifier to be used in this case is 'L".
Code for all the above cases is as follows. Uncomment the part you want to try and copy paste it in fprintf_function() below.
count = fprintf(fp,"%-10d %c",143,'k'); // Flag Example 1. cfunctionfile.txt will have : 143 k, Count = 12 in output. 143 will be followed by 7 blanks + 1 space and k
count = fprintf(fp,"%010d %c",143,'k'); // Flag Example 2. cfunctionfile.txt will have : 0000000143 k, Count = 12 in output. 0 padded to the left.
count = fprintf(fp,"%hd",1); // Length Example 1. cfunctionfile.txt will have : 1, Count = 1 in output
count = fprintf(fp,"%Lf",10.000000001223); // Length Example 2. cfunctionfile.txt will have : 10.000000, Count = 9 in output
count = fprintf(fp,"%4d",7); // Width Example 1. cfunctionfile.txt will have : 7, Count = 4 in output
count = fprintf(fp,"%15s","kbanyal"); // Width Example 1. cfunctionfile.txt will have : kbanyal (8 spaces before kbanyal), Count = 15 in output
count = fprintf(fp,"%10.3f",546666.7); // cfunctionfile.txt will have : 546666.700 , Count = 10 in output
count = fprintf(fp,"%3.4s","kbanyal"); // cfunctionfile.txt will have : kban , Count = 4 in output
Vugen C Code -
/* Output of following can be seen in the folder where this script is saved and in text file named - cfunctionfile.txt */
fprintf_function()
{
int count;
int fp = fopen("cfilefunctionfile.txt","w");
/* Opens a file for buffered I/O. File name is cfilefunctionfile.txt (will be created in script folder once executed.). Access mode is write "w". */
if (fp==NULL)
/* fopen() returns NULL if for some reason file was not opened. Make "cfilefunctionfile.txt" read only to see this if condition working. */
{
lr_error_message("File %s was not opened due to some error",fp);
}
count = fprintf(fp,"%d %e %f %s %u %c", 21, 2e12, 23.43, "Kbanyal", 007, 'k');
/* fprintf() will write to "cfilefunctionfile.txt" file if fopen() above was successfull and return number of characters written to file. */
lr_output_message("%d",count);
fclose(fp);
return 0;
}
1) Keeping track of which login ids from the given login ids were able to login to application.
2) If the script fails during run time and we want to record the group name and Vuserid for finding the root cause after the execution.
This function writes a fomatted output to stream
Declaration - int fprintf( FILE *fp, const char *fs [,arguments ] );
fp = Pointer to a file (actually stream)
fs = Formated string we want to write to the file (actually stream)
Return Value -
On sucess - Total number of characters printed.
On error - Negative number
fs or the formated string can be formated using different format tags prototype of which is %[flags][width][.precision][length]specifier
Most common specifiers are following:
%d = Displays and integer
%f = Displays a floating-point number in fixed decimal format
%e = Displays a floating-point number in exponential notation
%s = Displays string of characters
%u = Displays unsigned decimal integer
%c = Displays a character
Since most of the format tags will never be used and there are lots I am just giving one or two examples for all the tags below.
Flags = flags can be used for various purposes. Main is to format data as per requirment. For example -
1) - = is used to left justify the field.
2) 0 = to pad field with zeros rather than blanks.
Width = Every data format is provided with minimum required width to hold the same. Width format tag can be used to increase it further.
1) %[width]d - will increase the field width of given integer.
2) %[width]s - will increase the width of given string.
Precision = This format tag takes different meanings for different format types.
1) %[total].[decimal]f - Here total field length will be [total] and [decimal] of these will hold the decimal part for a float value.
2) %[minimum].[maximum]s - Here minimum width is [minimum] and maximum width is [maximum] for a string value. If string is more than [maximum]
value it will be cropped to [maximum] value.
Length = length modifier is used to let printf know that we want to print very big or very small variables. For example -
1) short int - Modifier to be uses in this case is 'h'. Short int is always takes less or equal bits as int. that is short int <= int <= long.
2) long double - Modifier to be used in this case is 'L".
Code for all the above cases is as follows. Uncomment the part you want to try and copy paste it in fprintf_function() below.
count = fprintf(fp,"%-10d %c",143,'k'); // Flag Example 1. cfunctionfile.txt will have : 143 k, Count = 12 in output. 143 will be followed by 7 blanks + 1 space and k
count = fprintf(fp,"%010d %c",143,'k'); // Flag Example 2. cfunctionfile.txt will have : 0000000143 k, Count = 12 in output. 0 padded to the left.
count = fprintf(fp,"%hd",1); // Length Example 1. cfunctionfile.txt will have : 1, Count = 1 in output
count = fprintf(fp,"%Lf",10.000000001223); // Length Example 2. cfunctionfile.txt will have : 10.000000, Count = 9 in output
count = fprintf(fp,"%4d",7); // Width Example 1. cfunctionfile.txt will have : 7, Count = 4 in output
count = fprintf(fp,"%15s","kbanyal"); // Width Example 1. cfunctionfile.txt will have : kbanyal (8 spaces before kbanyal), Count = 15 in output
count = fprintf(fp,"%10.3f",546666.7); // cfunctionfile.txt will have : 546666.700 , Count = 10 in output
count = fprintf(fp,"%3.4s","kbanyal"); // cfunctionfile.txt will have : kban , Count = 4 in output
Vugen C Code -
/* Output of following can be seen in the folder where this script is saved and in text file named - cfunctionfile.txt */
fprintf_function()
{
int count;
int fp = fopen("cfilefunctionfile.txt","w");
/* Opens a file for buffered I/O. File name is cfilefunctionfile.txt (will be created in script folder once executed.). Access mode is write "w". */
if (fp==NULL)
/* fopen() returns NULL if for some reason file was not opened. Make "cfilefunctionfile.txt" read only to see this if condition working. */
{
lr_error_message("File %s was not opened due to some error",fp);
}
count = fprintf(fp,"%d %e %f %s %u %c", 21, 2e12, 23.43, "Kbanyal", 007, 'k');
/* fprintf() will write to "cfilefunctionfile.txt" file if fopen() above was successfull and return number of characters written to file. */
lr_output_message("%d",count);
fclose(fp);
return 0;
}
No comments:
Post a Comment