Thread: Reading & calculating Mean from a Txtfile

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Moved to C programming forum.

    For your purposes, fscanf() will probably be the easiest approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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 ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03: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