Read file and add value


 
Thread Tools Search this Thread
Top Forums Programming Read file and add value
# 1  
Old 12-05-2012
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
Code:
#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 ;
}
}


Last edited by Franklin52; 12-05-2012 at 05:13 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 12-05-2012
you need:
myfile.getline(number,25); and atoi().
This User Gave Thanks to vistastar For This Post:
# 3  
Old 12-06-2012
@ 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
# 4  
Old 12-06-2012
Code:
#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:
Code:
11 23 33 561

# 5  
Old 12-06-2012
This will work with the original datafile:

Code:
#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);
}


Last edited by Corona688; 12-06-2012 at 11:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file and add it into part of file

Hello let me explain senario. there is a file which name is config and it store main software variable: file main.conf contents: update="1" log_login="0" allow_ports="" deny_ports="21,22,23" and there is a file which name is ports.txt file ports.txt contents: 25,26,27 i... (3 Replies)
Discussion started by: nimafire
3 Replies

2. Shell Programming and Scripting

Read a string and add " at multiple places

Hi All, I have below text, i want to add " in it, please advise. FORD MOTOR COMPANY FORD COMPONENT SALES, LLC Mazda Motor Manufacturing de Mexico, S.A. de C.V. Desired: "FORD MOTOR COMPANY" "FORD COMPONENT SALES, LLC"" Mazda Motor Manufacturing de Mexico, S.A. de C.V." edit by bakunin:... (2 Replies)
Discussion started by: Amit Saroha
2 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Shell Programming and Scripting

Read a file, add some text and send an email

Hi, If I am asking this question, you must have already figured out , that I am new to Unix, so here it goes I was trying to read a file, add some user defined content to it and send out an email , I did find out a way to achieve this, but looking at the code, it looks a bit crude to me, can... (3 Replies)
Discussion started by: karthikbhuvana
3 Replies

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Solaris

add a ftp user with read and write permissions on a directory

hi all how I can create an ftp user in solaris 10 and have read and write permission on a directory. Thanks. (1 Reply)
Discussion started by: luisfja
1 Replies

8. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. Shell Programming and Scripting

Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue. I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm. These files have 1 column with 1 value per line, like: 1.dhm ------ 10 20 30 40 50 2.dhm ------ 30 50 20 (3 Replies)
Discussion started by: dhuertas
3 Replies

10. Shell Programming and Scripting

I'm trying to read from a file and add to LDAP

Hi all I'm trying to write a script that reads from a file and then insert the record by a command line tool into LDAP. here is the file format FirstName LastName uid password and here is the script that I wrot: #!/bin/sh INPUT='/export/home/user/input' while read fname lname uid pass... (0 Replies)
Discussion started by: sh_ksa
0 Replies
Login or Register to Ask a Question