fprintf.c

来自「c21Examples.rar」· C语言 代码 · 共 52 行

C
52
字号
/* Demonstrates the fprintf() function. */
#include <stdlib.h>
#include <stdio.h>

void clear_kb(void);

int main( void )
{
    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);
    printf("\n");
    return 0;
}

void clear_kb(void)
/* Clears stdin of any waiting characters. */
{
    char junk[80];
    gets(junk);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?