Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to print 1 20 in an output file \"out txt\" Each number in the t

ID: 3925042 • Letter: W

Question

Write a program to print 1 20 in an output file "out txt" Each number in the text file should be in a new line Write a program that determines whether a pi en ear is a leap year or not Leap years are years in which February has 29 days Your program will ask. the user to enter a year continuously If the year entered is a leap sear your program will write the year in a txt file "leapvear txt", else the program will write the year in another txt tile "notleapyear txt" The user will have the option to exit the program by typing '-1'. Each year m the text files should be in .a new line Enter the following numbers to test 2000, 1800, 2008, 1900, 2009, 2013, 2 400, 2500 Create a txt file called "year.txt" that reads and writes all the contents of leapyear.txt and notleapyear.txt respectively.

Explanation / Answer

Please find the required program along with its output. Please see the comments against each line to understand the step.

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

int main()
{
FILE *fs1, *fs2, *ft;

char ch;

fs1 = fopen("leapyear.txt","r"); //open file leapyear.txt in read mode
fs2 = fopen("notleapyear.txt","r"); //open file notleapyear.txt in read mode

if( fs1 == NULL) { //print error and exit if leapyear.txt is cannot be opened
printf("Unable to open file leapyear.txt ");
return 0;
}

if( fs2 == NULL) { //print error and exit if notleapyear.txt is cannot be opened
printf("Unable to open file notleapyear.txt ");
return 0;
}



ft = fopen("year.txt","w"); //open file year.txt in write mode

if( ft == NULL) { //print error and exit if year.txt is cannot be created in write mode
printf("Error while creating file year.txt for writing. ");
return 0;
}

while( ( ch = fgetc(fs1) ) != EOF ) //get each char from first file and put(write) to new file
fputc(ch,ft);

while( ( ch = fgetc(fs2) ) != EOF ) //get each char from second file and put(write) to new file
fputc(ch,ft);

printf("Two files were merged into year.txt file successfully. ");

fclose(fs1); //close all opened files
fclose(fs2);
fclose(ft);

return 0;
}

--------------------------------------------

OUTPUT:

sh-4.3$ cat lsh-4.3$ cat leapyear.txt   
1990   
2000   

sh-4.3$ cat notleapyear.txt
2003   
2005   

sh-4.3$ main   
Two files were merged into year.txt file successfully.   
  
  
sh-4.3$ cat year.txt   
1990   
2000   
2003   
2005   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote