Sunday, 13 March 2016

C++ Program For Find The Sum of Integers In a File

Problem:- C++ adding numbers from an input file or C++ Adding Numbers From an Input File using File Handling or c++ read text file line by line. or c++ adding numbers from a file or c++ count numbers in a file or how to read numbers from a file in c++ or c++ average numbers from file or count number of integers in a file c++ or determining largest smallest & mean from file input or write a program that opens the file reads all the numbers from the file or c++ read numbers from file line by line c++ program to read and display text file line by line or 


Explanation:- First we have to create a file with extension (like .txt) after that by using the program we have to print the file in console screen. Always remember we have to read the file by line by line and also write the file sum line by line in another file name Sum.txt and one more thing file should be in the same folder in which our program already exists. We can give the file name in console screen so no need to give the filename in a program. So basically we have to take an input from one file and after the sum, we have to write the output in another file line by line. After the end of the program, one file will be created open the file you get the sum of the input file.


Solution:-

#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;
//Ghanendra Yadav
int main()
{
  ifstream inFile;
    char filename[20];

cout<<"Enter The File Name With Extension\n";
cin>>filename;

inFile.open(filename);

/*Here You Have To Create A File And put some data on it. 
Then Save the with Any Extension With File Name As Above Shown */
 
if (!inFile)
  {
    cerr << "File example.txt not found." << endl;
    return -1;
  }
  
  ofstream outFile("sum.txt");
  /*Here You Have Sum Of File Line By Line Sum  */
  string line;
  
  while (getline(inFile, line))
  {
    if (line.empty()) 
continue;

    istringstream iss(line);
    int sum = 0, next = 0;
    while (iss >> next) 
sum += next;
    outFile << sum << endl;
  }

  inFile.close();
  outFile.close();
  
  cout<<"File Created Successfully Go To Sum.txt File And Open\n";
  
  return 0;

}

Output:-

Input:-

C++ Program For Find The Sum of Integers In a File


Output:-

C++ Adding Numbers From a Input File using File Handling


File Output:-


C++ Program For Find The Sum of Integers In a File


You May Like This

1. C Program For Open A File (Or Open The Same Program Itself ) Itself Using File Handling

2. C++ Program To Merge Two File Into Third File Using File Handling

3. C++ Program For Copy One File To Another Using File Handling

4. C++ Program To Display The List Of Current Directory/Folder Using File

5. C++ Program For Count A Character In File Using File

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Store Employee Information And Display Using Structure

8. C Program For Find A Grade Of Given Marks Using Switch Case

9. C Program For HEAP Sort In Ascending Order Also Show Complexity

10. C++ Program For Selection Sort Using Function

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment