Read integers and appends sum to the end
Write a C program read integers and appends sum to the end in File Handling. Here’s simple program read integers and appends sum to the end in File Handling in C Programming Language.
Below is the source code for C program read integers and appends sum to the end in File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C program read integers and appends sum to the end in File Handling */ #include<stdio.h> #include<conio.h> #include<process.h> int main() { int a,i,n,sum=0; FILE *fp; //Writing numbers to the file fp=fopen("C:\\Users\\acer\\Documents\\file4.txt","w"); if(fp==NULL) { printf("File could not open!!"); exit(0); } printf("How many numbers? "); scanf("%d",&n); printf("\nEnter numbers in the file:\n"); for(i=0;i<n;++i) { scanf("%d",&a); putw(a,fp); } fclose(fp); //Reading the file and doing sum fp=fopen("C:\\Users\\acer\\Documents\\file4.txt","r"); if(fp==NULL) { printf("File could not open!!"); exit(0); } while((a=getw(fp))!=EOF) sum+=a; fclose(fp); //Appending sum to the file fp=fopen("C:\\Users\\acer\\Documents\\file4.txt","a"); if(fp==NULL) { printf("File could not open!!"); exit(0); } putw(sum,fp); fclose(fp); //Displaying file after append fp=fopen("C:\\Users\\acer\\Documents\\file4.txt","r"); if(fp==NULL) { printf("File could not open!!"); exit(0); } printf("\nFile after append:\n"); while((a=getw(fp))!=EOF) printf("%d ",a); fclose(fp); return 0; }
OUTPUT : :
/* C program read integers and appends sum to the end in File Handling */ How many numbers? 6 Enter numbers in the file: 1 2 3 4 5 6 File after append: 1 2 3 4 5 6 21 Process returned 0
Above is the source code for C program read integers and appends sum to the end in File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….
I every time used to read article in news papers but now as I am a user of web thus from
now I am using net for articles or reviews, thanks to web.