Programming in C
Programming in C
4th Edition
ISBN: 9780321776419
Author: Stephen G. Kochan
Publisher: Addison-Wesley
bartleby

Videos

Textbook Question
Book Icon
Chapter 16, Problem 1E

Type in and run the program presented in this chapter. Check the program’s results by comparing the original file you chose to copy with the filename you entered to copy and ensure the two are the same.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program Plan:

  • Include the required headers.
  • Define the structure
  • Define the main method
    • Declare the required variables.
    • Condition to validate the arguments that are passed.
    • Based on the condition that are is specified, proper resultant is being displayed.
Program Description Answer

The program is used to copy the contents of the text file 1 to the text file 2 by passing the arguments as the commands.

Explanation of Solution

//include the necessary headers

#include<stdio.h>

#include<stdlib.h>

//main method

int main(int argc,char *argv[])

{

//declare the required variables

FILE *in,*out;

int c;

//condition to validate the arguments that are passed

if(argc!=3)

{

//prompt user to enter the files

fprintf(stderr,"Need two files names\n");

//return the value

return 1;

}

//condition to validate the arguments that are passed

if((in=fopen (argv[1],"r"))==NULL)

{

//prompts the user that the file cannot be read

fprintf(stderr,"can't read %s.\n",argv[1]);

//return

return 2;

}

//condition to validate the arguments present

if((out = fopen(argv[2],"w"))==NULL)

{

//prompts user about the write error

fprintf(stderr,"can't write %s.\n",argv[2]);

//return value

return 3;

}

//loop that iterates for the end of file

while((c=getc(in))!=EOF)

//write the contents

putc(c,out);

//display file has been copied

printf("File has been copied.\n");

//close the file

fclose(in);

//close the file

fclose(out);

//return 0

return 0;

}

Sample Output

File has been copied.

  • The above code is executed by passing the test file in the form of commands.
  • Type the following for making the code to run:

Main.c test.txt test1.txt

test.txt:

Programming is fun

test1.txt:

Programming is fun

After executing bove commands the contents of the “test.txt” is being copied to “test1.txt” as shown above.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Implementation of an Integrated Inventory Management System at Green Fields Manufacturing” Green Fields Manufacturing is a mid-sized company specialising in eco-friendly home and garden products. In recent years, growing demand has exposed the limitations of their fragmented processes and outdated systems. Different departments manage production schedules, raw material requirements, and finished goods inventory using a patchwork of spreadsheets and older software tools. These silos create inconsistent data, errors in stock levels, delivery delays, and customer dissatisfaction.  Green Fields plans to implement an Integrated Inventory Management System to centralise production, procurement, inventory, and sales data to address these challenges. This technology aims to provide real-time visibility into stock levels, automate reorder points, and generate analytical dashboards for managers at both operational and strategic levels. Ultimately, the new system will streamline workflows, reduce…
. Differentiate between continuous and discrete systems. How does their nature affect the selection of simulation techniques?
hi, I need help to resolve the case, thank you

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
CMPTR
Computer Science
ISBN:9781337681872
Author:PINARD
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C - File I/O; Author: Tutorials Point (India) Ltd.;https://www.youtube.com/watch?v=cEfuwpbGi1k;License: Standard YouTube License, CC-BY
file handling functions in c | fprintf, fscanf, fread, fwrite |; Author: Education 4u;https://www.youtube.com/watch?v=aqeXS1bJihA;License: Standard Youtube License