Programming Fundamentals:File Handling (Session 7)
Character count
#include<stdio.h>#include<stdlib.h>int main(){char file1[100];int count=0;char c,ch,ck;FILE *fptr;printf("Enter the file name\n");scanf("%s",file1);fptr=fopen(file1,"r");printf("Enter the character to be counted\n");scanf(" %c",&c);ck=c;if((int)c>=65 &&(int)c<=90)c=(int)c+32;while((ch=getc(fptr))){if((int)ch>=65 && (int)ch<=90)ch=(int)ch+32;if(ch==EOF)break;else if(ch==c)count+=1;}printf("File '%s' has %d instances of letter '%c'.",file1,count,ck);fclose(fptr);return 0;}
File copy
#include <stdio.h>#include <stdlib.h>int main(){char ch, source_file[20], target_file[20];FILE *source, *target;printf("Enter the input file name\n");scanf("%s",source_file);source = fopen(source_file, "r");printf("Enter the output file name\n");scanf("%s",target_file);target = fopen(target_file, "w");while( ( ch = fgetc(source) ) != EOF )fputc(ch, target);fclose(source);fclose(target);return 0;}
Post a Comment