Question

In: Computer Science

Please dont copy from other answers. Do as simole as possible (C programming). code 1: create...

Please dont copy from other answers. Do as simole as possible (C programming).
code 1:
create a program that will copy the contents of a text file called (input.txt) to a file called (copied.txt). After the program runs the contents of both files should be the same. (im creating the input manually)

code 2:
Change the code from fist part so that instead of always copying from input.txt to copied.txt it instead asks the user to provide both file names.
thank you. Ill give you thumbs up!

Solutions

Expert Solution

Solution

Code 1 - The code has been given below along with the output. Comments have been placed to depict the functionality.

#include <stdio.h>
int main()
{
    /*Create File Pointers*/
    FILE *fle, *flecopy;
    char ch;
    /*Open input file for reading */
    fle = fopen("input.txt", "r") ; 
    /* open output file in writing mode, file will be created if not existing*/
    flecopy = fopen("copied.txt","w");

    /*Return message if there is a problem opening the file*/
    if ( fle == NULL ) 
    { 
        printf( "Error Opening the file" ) ; 
    } 
    else
    { 
           while ( 1 )  
            {  
                /*Read file character by character*/
                ch = fgetc ( fle ) ;  
                if ( ch == EOF )  
                    break ;  
                /*Copy the character to the copied file*/    
                fputc(ch,flecopy) ;  
            } 
        /*Close the files*/                
        fclose (fle ) ;  
        fclose (flecopy); 
    }
    return 1;
}

Output:

input.txt = "This is the input file, its contents are to be copied."

copied.txt = Not existing before the program run.

copied.txt (after program run) = "This is the input file, its contents are to be copied."

Code 2 : Modified code where file names are given by the user.

#include <stdio.h>


int main()
{
    char inp[50],outp[50];
    /*ask user for the file names */
    printf("Enter the input file name : ");
    scanf("%s",inp);

    printf("Enter the file name to which contents are to be copied: ");
    scanf("%s",outp);

    /*Create File Pointers*/
    FILE *fle, *flecopy;
    char ch;
    /*Open input file for reading */
    fle = fopen(inp, "r") ; 
    /* open output file in writing mode, file will be created if not existing*/
    flecopy = fopen(outp,"w");

    /*Return message if there is a problem opening the file*/
    if ( fle == NULL ) 
    { 
        printf( "Error Opening the file" ) ; 
    } 
    else
    { 
           while ( 1 )  
            {  
                /*Read file character by character*/
                ch = fgetc ( fle ) ;  
                if ( ch == EOF )  
                    break ;  
                /*Copy the character to the copied file*/    
                fputc(ch,flecopy) ;  
            } 
        /*Close the files*/                
        fclose (fle ) ;  
        fclose (flecopy); 
    }
    return 1;
}

Ouput:

Enter the input file name : input.txt
Enter the file name to which contents are to be copied: newcopiedfile.txt

After the program runs the contents of the newcopiedfile.txt

"This is the input file, its contents are to be copied."


Related Solutions

Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1. List the following functions according to their order of growth from the lowest to the highest: (n−2)!, 5lg(n+100)10, 22n, 0.001n4 +3n3 +1, ln2 n, √3 n, 3n. 2. The range of afinite nonempty set of...
Please do not copy from other post or the copy from the web Creating Sunburst: Legal...
Please do not copy from other post or the copy from the web Creating Sunburst: Legal and Organizational Considerations Ravi, who has a degree from State University in mechanical engineering, was previously employed by a start-up firm in New York City. He worked long hours for little pay, but when the business sold, his stock options paid off. As a result, he now has some time and $100,000 to invest in Sunburst. More importantly, Ravi has the knowledge and know-how...
PLEASE TYPE THE ANSWER AND KEEP IT SHORT AND SIMPLE!!!! DO NOT COPY OTHER ANSWERS! Contrast...
PLEASE TYPE THE ANSWER AND KEEP IT SHORT AND SIMPLE!!!! DO NOT COPY OTHER ANSWERS! Contrast Newton's model of the universe with the descriptive models of Ptolemy, Copernicus, and Kepler.
Please answers on your own words don't copy from other writers in this website or from...
Please answers on your own words don't copy from other writers in this website or from other websites I need 0% plagiarism​​​​​​​. Note. please don't copy from other writer here in Chegg or other web because it will be plagiarism​​​​​​​ coz maybe some student already submitted so it will match so please if you will copy from other source don't answer.just make sure its your own words. thanks answer the following: A. A company wants to implement good internal control....
PLEASE DO NOT USE OTHER ALREADY GIVEN ANSWERS TO ANSWER THIS BY COPY PASTING. Suppose you...
PLEASE DO NOT USE OTHER ALREADY GIVEN ANSWERS TO ANSWER THIS BY COPY PASTING. Suppose you are the economic advisor of a fictitious president who lives in a world with only one time period. Consumers and firms in this economy are similar to the consumers and firms studied. Assume that the substitution effect is stronger than the income effect. The government can only collect revenue through lump-sum taxes. Suppose you are in a meeting and the president tells you the...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
It is a simple discussion please dont copy from any website, Ii need to be at...
It is a simple discussion please dont copy from any website, Ii need to be at least 3 paragraph. thanks Discussion "The Women's Kingdom" Please watch "The Women's Kingdom" which is a video about a matrilineal group in China and this video will give you an insight into a very different marriage pattern that most students are familiar with. After you have watched the video, research and post on a marriage pattern of a particular culture that is different from...
Please Plagirism is Prohibited kindly do in your own words dont copy please internet . Prepare...
Please Plagirism is Prohibited kindly do in your own words dont copy please internet . Prepare a detailed report on the following legal aspects in context to UAE labour laws: 1. Duties and obligations of the employer in a contract of employment [5 marks] 2. Duties and obligation of the employee in a contract of employment [5 marks] 3. Evaluate the legal process to an employment dispute in UAE labour law context [5 marks]
please dont copy from internet. thank you Assignment # 12 Q 1: Prepare journal entries to...
please dont copy from internet. thank you Assignment # 12 Q 1: Prepare journal entries to record the following merchandising transactions of Blink Company, which applies the perpetual inventory system. (Hint: It will help to identify each receivable and payable; for example, record the purchase on July 1 in Accounts Payable—Boden.) July 1 Purchased merchandise from Boden Company for $6,000 under credit terms of 1/15, n/30, FOB shipping point, invoice dated July 1. 2 Sold merchandise to Creek Co. for...
Briefly discuss the concept of International Political Economy (please do not copy from internet. if possible...
Briefly discuss the concept of International Political Economy (please do not copy from internet. if possible please provide a couple of in-text citation from a valid resource like a journal or a book)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT