Question

Please write this in C.

Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an o

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <stdio.h>
#include<strings.h>

int main()
{
    //for counting the no of words
    int count=0;
    //variables for taking file name from user
    char file1[50],file2[50],file3[50];

    //variables for reading words from the 2 files
    char word1[100],word2[100];
  
    //file pointers for 2 input file and 1 output file
    FILE *f1,*f2,*f3,*out;
  
    //take file name from user
    printf("Enter first input file name: ");
    scanf("%s",file1);
  
    //open 1st file
    f1 = fopen(file1,"r");

    //if opening fails
    if(f1 == NULL)
    {
        //print message and exit
        printf("Error opening input file: %s",file1);
        exit(1);           
    }


    printf("Enter second input file name: ");
    scanf("%s",file2);
     f2 = fopen(file2,"r");

    if(f2 == NULL)
    {
         printf("Error opening input file: %s",file2);
            exit(1);           
    }

    printf("Enter output file name: ");
    scanf("%s",file3);
    //open output file in read mode to see that it can be opened
    f3 = fopen(file3,"r");
  
    if(f3 == NULL)
    {
      printf("Error opening output file: %s",file3);
      exit(1);           
    }
    //close the file opened in read mode
    fclose(f3);
  
    //reopen the output file in write mode
    out=fopen(file3,"w");

   printf("Interleaved text:\n");

   //read the input files till they both have words
   //run while loop till any of the files all words are read
    while(fscanf(f1,"%s",word1)!=EOF && fscanf(f2,"%s",word2)!=EOF)
   {       //display file1 word
           printf(" %s",word1);
           //increase the count value by 1
           count++;
            //write in output file
            fprintf(out," %s",word1);
          
           //display file 2 word
           printf(" %s",word2);
           //increase the count value by 1
           count++;
           //write in output file
            fprintf(out," %s",word2);
      
   }
  
    //the below two while loop will be executed if one of the file has more no of words then the other
  
   //if file1 has not reach its end
   while(fscanf(f1,"%s",word1)!=EOF){
        printf(" %s",word1);
       count++;
       fprintf(out," %s",word1);
  
   }
   //if file2 has not reach its end
   while(fscanf(f2,"%s",word2)!=EOF){
        printf(" %s",word2);
       count++;
       fprintf(out," %s",word2);
  
   }
   //display the count variable
   printf("\n%d words written.",count);
  
   //close the files
    fclose(f1);
    fclose(f2);
    fclose(out);
  
    return 0;
}

OUTPUT:

nter first input file name: F1.TXT er second input file name: F2.TXT nter output file n Interleaved text: Copy some and then

OUT.TXT:

Copy some and then interleave these 2 text files. without losing any words

Add a comment
Know the answer?
Add Answer to:
Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a program that asks the user for the name of an image, the name of an output file. Your pro...

    Write a program that asks the user for the name of an image, the name of an output file. Your program should then save the upper right quarter of the image to the output file specified by the user. A sample run of your program should look like: Enter image file name: hunterLogo.png Enter output file: logOUR.png which would have as input and output: HUNTER ITER The City University of New York Hint: See sample programs from Lectures 3 and...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • For Python | Instructions Write a script named difpy. This script should prompt the user for...

    For Python | Instructions Write a script named difpy. This script should prompt the user for the names of two text files and compare the contents of the two files to see if they are the same. 1. If they are the script should simply output "Yes". 2. If they are not the script should output "No", followed by the first lines of each file that differ from each other The input loop should read and compare lines from each...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • Write a program in C++ that prompts the user to input the name of a text...

    Write a program in C++ that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT