All articles
c

A program to write integers in a file

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 116

  • Write a program to write integers in a file

Program

#include <stdio.h>
#include <stdlib.h>
 
void main()
{
    FILE *fp;
    int c;
 
    fp = fopen("integers.txt", "w");
    if (fp == NULL)
    {
        printf("Error: unable to create integers.txt\n");
        exit(1);
    }
 
    printf("Type the content to store in integers.txt ... (Press CTRL+D to trigger EOF)\n\n");
    while ((c = getchar()) != EOF)
    {
        putc(c, fp);
    }
    fclose(fp);
 
    printf("\n");
}

Result

Type the content to store in integers.txt ... (Press CTRL+D to trigger EOF)

12 54 67 987 32 497 5
62

Comments