Create a file and call it testdata3 The file should contain

Create a file and call it testdata3. The file should contain an unknown number of integer values. Write a program that will read in these numbers until end of file is reached and then outputs the sum of the numbers. You should not use an array to do this problem.

Solution

// java programming solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigInteger;
import java.util.StringTokenizer;


public class SumFromATextFile {
   public static void main(String [] args ){
       BigInteger sum = new BigInteger(\"0\");//for big size of data
       //int sum = 0;
       StringTokenizer data = new StringTokenizer(\" \");
       try {
       BufferedReader br = new BufferedReader(new FileReader(\"testdata3.txt\")); // read the file
      
      
       String line = br.readLine(); // read the next line
      
       while (line != null) {
           data = new StringTokenizer(line,\" \");// number after space
           while (data.hasMoreElements()) {
               String s = data.nextToken();
                   if(s.matches(\"^(-?)\\\\d+$\")){
  System.out.println(\"Num : \"+s);
                       sum = sum.add(new BigInteger(s));
                       //sum = sum + Integer.parseInt(s);
                   }
                   }
         
       line = br.readLine();
       }
      
         
       System.out.println(\"Sum of the integer in the file is : \"+sum);
       br.close();
       }catch(Exception e){
           System.out.println(\"File Not found Exception \");
       }
      
   }
}

// C Programming solution

#include <iostream.h>
#include <fstream.h>
#include<conio.h>
#include<process.h>
#include<math.h>

//using namespace std;

void main(void)
{ clrscr();
//input file name
char filename[] = \"testdata3.txt\";
ifstream inputfile;
   int num;

       int sum=0;

// opening input file for reading
inputfile.open(filename, ios::in);
// test if fail to open the file, do…
// error handling for file opening
if(inputfile.fail())
{
   cout<<\"Opening file \"<<filename<<\" for reading\ \";
   cout<<\"------------------------------------------\ \";
   cout<<\"The file could not be opened!\ \";
   cout<<\"Possible errors:\ \";
   cout<<\"1. The file does not exist.\ \";
   cout<<\"2. The path was not found.\ \";
       exit(0); // 1-just exit
   // 0-normal, non zero - some error
}
// if successful, do the following...
else
{
   cout<<\"The \"<<filename<<\" file was opened successfully!\ \";
   // declare some variables for simple calculation
   // read data from input stream...
   inputfile>>num;
   // test, if end of file not found, do the following...
   while(!inputfile.eof())
   {

           sum+=num;

   // re-read the next item price within the loop
   inputfile>>num;
}
cout<<\"The Sum is : \"<<sum<<endl;
cout<<\"\ -------DONE-------\ \"<<endl;
// close the input file
inputfile.close();
// test closing file, if fail to close the file, do...
// error handling for file closing

if(inputfile.fail())
{
   cout<<\"The \"<<filename<<\" file could not be closed!\ \";
   // something wrong, just exit...
   exit(1);
}
// if successful close the file, do....
else
   cout<<\"The \"<<filename<<\" file was closed successfully!\ \";
}


getch();
}

 Create a file and call it testdata3. The file should contain an unknown number of integer values. Write a program that will read in these numbers until end of
 Create a file and call it testdata3. The file should contain an unknown number of integer values. Write a program that will read in these numbers until end of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site