Advertisement
Shailrshah

Sum of an Array stored in data.txt

Apr 20th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.41 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.  
  5.     int limit, i ,n, sum=0;
  6.     FILE *fp;
  7.     fp = fopen("C:\\DATA.TXT","w");
  8.  
  9.     printf("Enter number of elements: ");
  10.     scanf("%d",&limit);
  11.     printf("Enter %d elements:-\n",limit);
  12.     for(i=0;i<limit;i++)
  13.     {
  14.         scanf("%d",&n);
  15.         putw(n,fp);
  16.  
  17.     }fclose(fp);
  18.  
  19.     fp=fopen("c:\\Data.txt","r");
  20.     while((n=getw(fp))!=EOF)
  21.         sum+=n;
  22.     printf("Sum = %d",sum);
  23.     fclose(fp);
  24.    
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement