Reading & calculating Mean from a Txtfile

This is a discussion on Reading & calculating Mean from a Txtfile within the C Programming forums, part of the General Programming Boards category; How to calculate a mean value from a file. my txt file contains the following values( No floating point only ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Reading & calculating Mean from a Txtfile

    How to calculate a mean value from a file. my txt file contains the following values( No floating point only integers). Assume that my data.txt file contains 100 numbers.

    Input File name: Data.txt- Values 2, 3, 4, 5, 6, 7, 8, 9

    Output
    --------
    Mean = 5.5

    I m new to Files. can anybody help me to solve this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Suppose that you used std::cin to read in input entered by the user in the same format as that of the file. What would you do?
    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 2009
    Posts
    16
    No, i don't want to read from STDIN. have to read from a txt file. can you please help me? I m new to file operation.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Quote Originally Posted by cdoubts
    No, i don't want to read from STDIN. have to read from a txt file. can you please help me?
    After you show us how you read from standard input using std::cin (without interactive input, i.e., do not ask the user to re-enter if invalid input is detected, etc), we'll tell you how a slight change of your code can allow you to read from an input file stream.
    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

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    I know simple file operations. fgetc () reads char by char. how can i calculate mean here?
    ps:I don't want any input from user (STDIN)"

    Insert
    Code:
    #include<stdio.h>?
    main()
    {
    int sum=0,mean=0;
    char ch;
    int number[10],i=0,count=0;
    char comma[10];
    char buffer[100];
    int got;
    FILE *fp1;
    clrscr();
    fp1=fopen("c:\\TC\\BIN\\data.txt","r");
    while((ch=fgetc(fp1))!=EOF)
    printf("%c",ch);
    getch();
    }
    
    /*while(fgets(buffer,100,fp1)!=NULL)
    printf("%s",buffer);*/
    /*{
    got=sscanf(buffer,"%d %s",&number[i],&comma[i]);
    printf("Test: %d %d",number[i],got);
    i++;
    } */
    /*while((ch=fgetc(fp1))!=EOF)
    {
    number[i]=
    if(!(ch==','))
    printf("%c",ch);
    }  */

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Quote Originally Posted by cdoubts
    I know simple file operations. fgetc () reads char by char. how can i calculate mean here?
    Have you not learnt how to use std::cin and C++ I/O streams?
    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

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    16
    I want to solve this problem in C programming. i don't want to do in C++.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Moved to C programming forum.

    For your purposes, fscanf() will probably be the easiest approach.
    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

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    16
    Do i need to post this again in C programming forum?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Suppose you show us how you'd read it from stdin, and then just change those same functions to be theif f-ing version? Because really, that's what you need to do. You make one function read all your data. You make another perform the work on it.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    16
    I m not reading from a stdin. (i don't want to do as well). i have to read only from a Txt file which contains the above data.

    I read about files. i really don't know how to read a comma separated integer value from a TXT file. if i can read this, i can easily calculate the mean

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point here. If you can do it with the same input, skipping the entire "file" aspect of it, then you can do it WITH the file aspect.

    What part don't you know how to do anyway, other than indent?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    16
    you are right. i m not able to do with the sample input as well. if my input is 345, 23, 1, 78 then how should i do? can you please help me?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know how format strings work in C? If you wanted to read an integer and a comma, for instance, you could use "%d," as that would read in an integer and then expect a comma. (Nothing bad will necessarily happen in this case if there's no comma -- if you had a bunch of them, and a comma was missing, it would stop there and not go on.) Now to do a 100 of them....

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    16
    I tried, but its not working. Take for example if i read 44,55,66 from a keyboard then calculating mean. my problem is how to read these datas (i m able to read, how to split 44 55 & 66). can you please send me sample code ?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  4. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  5. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM

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