Read file and add value

i have a file outfile.txt which contain
12
22
i have written this program to read the file and show the output,but i dont know how to add these value and show the total.
my-codes are

#include<cmath>
#include<cstdlib>
#include<iostream>
#include<fstream>
using namespace std;
int main ()
{
char filename [25];
ifstream  myfile;
cin.getline(filename,25);
myfile.open(filename);
if (!myfile.is_open())
{
exit (0);
}
char    number [25];
myfile >> number;
while (myfile.good ())
{

cout << number << " " << "\n";
myfile >> number ;
}
}

you need:
myfile.getline(number,25); and atoi().

1 Like

@ vistastar= thank you.

---------- Post updated 12-06-12 at 01:39 AM ---------- Previous update was 12-05-12 at 11:41 AM ----------

i have done that
but still i m unable to add the both value.
how program can read each value separately and add it ?
like num1=12 num2 =22 num3= 34

#include<cmath>
#include<cstdlib>
#include<iostream>
#include<fstream>
using namespace std;
int main ()
{
char filename [25];
ifstream  myfile;
cin.getline(filename,25);
myfile.open(filename);
if (!myfile.is_open())
{
exit (0);
}
int sum = 0, value;

while (myfile >> value)
       sum += value;
cout<<sum;
}

input file like:

11 23 33 561

This will work with the original datafile:

#include <stdio.h>

int main(void)
{
        int total=0, n;
        char buf[512];
        FILE *fp;

        fgets(buf, 512, stdin);
        for(n=0; buf[n]; n++) if((buf[n] == '\n') || (buf[n] == '\r')) buf[n]=0;

        if((fp=fopen(buf, "r")) == NULL) return(1);

        while(fgets(buf, 512, fp) != NULL) if(sscanf(buf, "%d", &n) == 1) total += n;

        fclose(fp);
        printf("Total is %d\n", total);
}