Department of Computer Science and Engineering
Department of Computer Science and Engineering
Exercise # 11
1. Sample Program to reading, writing to a file and printing the
contents of the file.
#include <stdio.h>
void readfile(void);
void writefile(void);
void printfile(void);
int main()
{
int ch;
printf("*************File operations Menu******************* \n");
printf("1. Read and Print file \n");
printf("2. Read and Write file \n");
printf("3. Print file \n");
printf("Enter Choice[1/2/3]\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
readfile();break;
case 2:
writefile();break;
case 3:
printfile();break;
}
return 0;
}//main
void readfile()
{
printf("Enter the file name\n");
scanf("%s",rfn);
f1=fopen(rfn, "r");
while(!feof(f1))
printf("%c", fgetc(f1));
}//readfile
void writefile()
{
char c;
f1=fopen(rfn, "r");
f2=fopen(wfn, "w");
while(!feof(f1))
{
c= fgetc(f1);
fputc(c,f2);
}//while
}//readfile
void printfile()
{
printf("Enter the file name to print\n");
scanf("%s",rfn);
f1=fopen(rfn, "r");
while(!feof(f1))
printf("%c", fgetc(f1));
}//readfile
#include <stdio.h>
FILE *in;
int main()
{
int op1,op2;
char oper;
in = fopen("oper.txt", "r");
while(!feof(in))
{
fscanf(in, "%d\t%c\t%d", &op1,&oper,&op2);
// printf("The got values are %d\t%c\t%d\n",op1,oper,op2);
switch(oper)
{
case '+':
printf("The Addition of %d and %d is %d\n", op1, op2,
op1+op2);
break;
case '*':
printf("The Multiplication of %d and %d is %d\n", op1, op2,
op1*op2);
break;
}//switch
}//while
fclose(in);
return 0;
}
5 + 10
5 * 12
#include <stdio.h>
FILE *f1;
f1=fopen(argv[1], "r");
while(!feof(f1))
printf("%c", fgetc(f1));
return 0;
}//main
JOBS