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

The Problem: You\'ll find a poem I\'ve been working on for the past several mont

ID: 3919581 • Letter: T

Question

The Problem:

You'll find a poem I've been working on for the past several months attached to this announcement as "input.txt". It reads as follows:

red
orange
yellow
green
magenta
blue
indigo
violet

What I'd like for you to do for me is download input.txt and write a program that accomplishes the following:

1. Create a new file called output.txt. This is where your output will be written to.

2. Give my poem a title. This should be the first line written to output.txt, and it can be whatever you'd like.

3. Read the contents of input.txt into a buffer, one line at a time. Print them out to the console and then write them to output.txt.

4. Correct the mistake in my poem! If you look closely, you'll notice that my poem draws heavily from the colors of the rainbow. Magenta is not one of these colors, so make sure that it does not get written to output.txt (hint: you'll probably want to handle this using a conditional and strcmp() when you write to output.txt).

5. Give my poem a closing line to tie everything together. Again, this can be whatever you'd like.

Sample Output:

Your output.txt file should look something like this:

a poem by bob smith
red
orange
yellow
green
blue
indigo
violet
these are the colors of the rainbow.

Notice the title, omission of the word magenta, and the closing line at the end of the poem.

Please use file i/o and the c language!!

Explanation / Answer

#include<stdio.h>
#include<string.h>

int main(){

    FILE *fp, *fp1;
    char word[50];

    fp = fopen("input.txt","r");
    if (fp == NULL){
       printf("Error opening input file ");
       return 0;
    }
    fp1 = fopen("output.txt","w");
    if (fp1 == NULL){
       printf("Error opening output file ");
       return 0;
    }
    fprintf(fp1,"%s ","a poem by bob smith");
    while(!feof(fp)){
        fscanf(fp,"%s",word);
        if (strcmp(word,"magenta") != 0){
           fprintf(fp1,"%s ",word);
        }
    }
    fprintf(fp1,"%s","these are the colors of the rainbow.");
    fclose(fp);
    fclose(fp1);
}

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