How To Sum undetermined number from a txt File in C

This is a discussion on How To Sum undetermined number from a txt File in C within the C Programming forums, part of the General Programming Boards category; Hi i have problem with using TXT file in C. Actually, i have a txt file stored in C:\ . ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    How To Sum undetermined number from a txt File in C

    Hi
    i have problem with using TXT file in C. Actually, i have a txt file stored in C:\\ . It contains numbers. i don't know how many number i have in this TXT File. i want a program which opens the file and sum the number. would you help me with that ?

    Thank You

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    If you just need the sum, then it seems quite trivial: read one number at a time and add it to the current sum. After you are done reading the entire file, you would have the sum of the numbers, regardless of how many there were (assuming that the integer type you chose has a range large enough to store this sum).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Quote Originally Posted by laserlight View Post
    If you just need the sum, then it seems quite trivial: read one number at a time and add it to the current sum. After you are done reading the entire file, you would have the sum of the numbers, regardless of how many there were (assuming that the integer type you chose has a range large enough to store this sum).
    Thank you for your concern.
    Actually, i wrote this code for reading 10 numbers from a txt file and calculates the AVG of the numbers. Now, i want to do the same for n (dont know how many numbers we have) . how can i change the code ?
    thank you

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    
    
    int main()
    {
    
    FILE *fp;
    int i;
    int s=0;
    int a;
    
    fp=fopen("D:\\a.txt", "r+");
    if (fp!=0)
    {
    for(i=0; i<=9; i++)
       {
       fscanf(fp,"%d",&a);
       s+=a;
       }
    printf("%d", s/10);
    int mkji = fclose(fp);
    }
    else
        printf("FILE CANNOT BE OPEN");
    
    getch();
    return 0;
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Hints:
    1. You can count the number of numbers while reading.
    2. Make use of the fact that fscanf(fp,"%d",&a) == 1, then the read was successful.
    3. Use better variable names.
    4. Indent your code properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string of undetermined length
    By burqqq in forum C Programming
    Replies: 5
    Last Post: 03-21-2010, 09:06 AM
  2. Performing File operations using File Inode number
    By rak1986 in forum C Programming
    Replies: 4
    Last Post: 09-22-2008, 03:43 AM
  3. nested while-loop + undetermined error
    By azsquall in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2008, 01:21 PM
  4. UNdetermined Destiny
    By Data in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 12-02-2005, 02:27 AM
  5. Passing an undetermined amount of variables.
    By Jeffrey in forum C Programming
    Replies: 1
    Last Post: 12-22-2003, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21