This program stores a sentence entered by the user in a file.
#include <stdio.h>
#include <stdlib.h>
int main() {
    char sentence[1000];
    // creating file pointer to work with files
    FILE *fptr;
    // opening file in writing mode
    fptr = fopen("program.txt", "w");
    // exiting program 
    if (fptr == NULL) {
        printf("Error!");
        exit(1);
    }
    printf("Enter a sentence:\n");
    fgets(sentence, sizeof(sentence), stdin);
    fprintf(fptr, "%s", sentence);
    fclose(fptr);
    return 0;
}
Output
Enter a sentence: C Programming is fun Here, a file named program.txt is created. The file will contain C programming is fun text.
Comments
Post a Comment