Program to write multiple lines to a text file.

Here, we have a basic program example to write multiple lines into a text file using different languages. This program is created in c language, c++, Java, and Python.

Code to write multiple lines in a file in C language

#include <stdio.h>
int main ()
  FILE * fptr;
  int i,n;
  char str[100];
  char fname[20]="new.txt";
  char str1;
	printf(" Input the number of lines to be written : ");
	scanf("%d", &n);
	printf("\n :: The lines are ::\n");
	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("\n The content of the file %s is  :\n",fname);
	str1 = fgetc(fptr);
	while (str1 != EOF)
			printf ("%c", str1);
			str1 = fgetc(fptr);
    fclose (fptr);
    return 0;

Code to write multiple lines in a file in Python language

myLines = ["Hi!", "This is body", "Thank you"]
with open('Desktop/readme.txt', 'w') as f:
    for line in myLines:
print("New text file created successfully!")