home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tyc
/
list16_2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-16
|
1KB
|
50 lines
/* Demonstrates the fprintf() function. */
#include <stdio.h>
void clear_kb(void);
main()
{
FILE *fp;
float data[5];
int count;
char filename[20];
puts("Enter 5 floating point numerical values.");
for (count = 0; count < 5; count++)
scanf("%f", &data[count]);
/* Get the filename and open the file. First clear stdin */
/* of any extra characters. */
clear_kb();
puts("Enter a name for the file.");
gets(filename);
if ( (fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Error opening file %s.", filename);
exit(1);
}
/* Write the numerical data to the file and to stdout. */
for (count = 0; count < 5; count++)
{
fprintf(fp, "\ndata[%d] = %f", count, data[count]);
fprintf(stdout, "\ndata[%d] = %f", count, data[count]);
}
fclose(fp);
}
void clear_kb(void)
/* Clears stdin of any waiting characters. */
{
char junk[80];
gets(junk);
}