Program to write content into the file

Write a program to Write a Sentence to a File.
C Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[1000];
    FILE *fp;
    fp = fopen("hello.txt""w");

    if (fp == NULL)
    {
        printf("File is  not opened\n");
        return -1;
    }
    printf("Enter a line to add into the file:\n");
    fgets(strsizeof(str), stdin);
    fprintf(fp"%s"str);
    fclose(fp);
    return 0;
}

Input:

Enter a line to add into the file:
Coding is fun

Output:
file hello.txt contains : Coding is fun


No comments:

Post a Comment