Writing multiple lines in a text file || File handling || C programming
In this program, we are going to Write multiple lines in a text file with the help of C Programming Language.

 

#include <stdio.h>
int main()
{
    FILE *fptr;
    int i, n;
    char str[100];
    char fname[50] = "test.txt";
    char str1;
    printf(" Enter the number of lines you want to enter:\n ");
    scanf("%d", &n);
    printf("Enter what you want to enter");
    fptr = fopen(fname, "w");
    for (i = 0; i < n + 1; i++)
    {
        fgets(str, sizeof str, stdin);
        fputs(str, fptr);
    }
    fclose(fptr);
    fptr = fopen(fname, "r");
    printf("You entered lines are %s is  :\n", fname);
    str1 = fgetc(fptr);
    while (str1 != EOF)
    {
        printf("%c", str1);
        str1 = fgetc(fptr);
    }
    fclose(fptr);
    return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post