Read all salary of employee which is stored in input.txt file and sum it and store the "sum of all salary" in output.txt file
input.txt file
Vineel Phatak, 520, NOBODY
Ajay Joshi, 250, Vineel Phatak
Abhishek Chauhan, 120, Ajay Joshi
Jayesh Godse, 500, NOBODY
Vijaya Mundada, 60, Abhishek Chauhan
Shital Tuteja, 45, Jayesh Godse
Rajan Gawli, 700, Vineel Phatak
Zeba Khan, 300, Jayesh Godse
Chaitali Sood, 100, Zeba Khan
Sheila Rodrigues, 35, Vineel Phatak
output
2630
solution
input.txt file
Vineel Phatak, 520, NOBODY
Ajay Joshi, 250, Vineel Phatak
Abhishek Chauhan, 120, Ajay Joshi
Jayesh Godse, 500, NOBODY
Vijaya Mundada, 60, Abhishek Chauhan
Shital Tuteja, 45, Jayesh Godse
Rajan Gawli, 700, Vineel Phatak
Zeba Khan, 300, Jayesh Godse
Chaitali Sood, 100, Zeba Khan
Sheila Rodrigues, 35, Vineel Phatak
output
2630
solution
#include<stdio.h> #include<ctype.h> #include<string.h> FILE *fp; static int sal_sum=0; void sum(char a[]){ int tmp = atoi(a);//to convert string to integer => atoi sal_sum += tmp; } int main(){ char str[100]; clrscr(); fp=fopen("input.txt","r"); while(!feof(fp)){ fscanf(fp,"%s",str); if(isdigit(str[0])){//check if the string is digit sum(str); } } fclose(fp); fp = fopen("output.txt","w"); fprintf(fp,"%d",sal_sum); fclose(fp); return 0; }
Comments
Post a Comment