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

C PROGRAM (PLEASE NOT C++ OR JAVA) Write a program that opens the input file “in

ID: 3694252 • Letter: C

Question

C PROGRAM (PLEASE NOT C++ OR JAVA)

Write a program that opens the input file “inputfile.txt” and copies the file to the screen.

Write a program that reads an unknown number of data values from the user (less than 1000 values, all integers) until the sentinel value -999 is read, then writes the data into an output file called “outputfile.txt” as a series of integers each on its own line.

Write a program that asks the user for the name of two input files and appends the data from the second file onto the first file. To be clear, the second file should be unchanged, and the first file should end up holding all its own data followed by all the data from the second file. Be sure to handle the potential error of a missing data file appropriately.

Explanation / Answer

Write a program that opens the input file “inputfile.txt” and copies the file to the screen.

#include <stdio.h>

#include <conio.h>

void main()

{

FILE *fp;

char ch;

clrscr();

/* open a file*/

fp=fopen("input.txt","r");

if(fp==NULL)

{

printf(" CAN NOT OPEN FILE");/*file doesnot exist*/

getch();

exit();

}

do

{

ch=getc(fp);

printf("%c",ch);

}

while(ch!=EOF);

fclose(fp); getch();

}

Write a program that reads an unknown number of data values from the user (less than 1000 values, all integers) until the sentinel value -999 is read, then writes the data into an output file called “outputfile.txt” as a series of integers each on its own line.

#include<string.h>

#include <stdio.h>

#include<stdlib.h>

int main(){

char name[50];

int i,n,marks;

FILE *fptr;

fptr=(fopen("C://Newfile.c","w"));

if(fptr==NULL){

printf("Error!");

exit(1);

}

while(!strcmp(name,"-999"))

{

printf("enter a datavalue");

scanf("%s",name);

fprintf(fptr," Name: %s ",name);

}

fclose(fptr);

return 0;

}

Write a program that asks the user for the name of two input files and appends the data from the second file onto the first file. To be clear, the second file should be unchanged, and the first file should end up holding all its own data followed by all the data from the second file. Be sure to handle the potential error of a missing data file appropriately.

#include<stdio.h>

void main()

{

FILE *fp1,*fp2;

char ch,fname1[20],fname2[20];

printf(" enter sourse file name");

gets(fname1);

printf(" enter sourse file name");

gets(fname2);

fp1=fopen(fname1,"r");

fp2=fopen(fname2,"a");

if(fp1==NULL||fp2==NULL)

{

printf("unable to open");

exit(0);

}

do

{

ch=fgetc(fp1);

fputc(ch,fp2);

}

while(ch!=EOF);

fclose(fp1);

fclose(fp2);

}