Sunday, 13 March 2016

Write A C++ Program For Read A No. From File And Sum Of No. Line By Line Using File

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

using namespace std;
//Ghanendra Yadav
int main()
  ifstream inFile("example.txt");
/*Here You Have To Create A File And Write Into File.
 What You Want Then Save 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;

  cout<<"File Created Successfully Go To Sum.txt File And Open\n";
  return 0;



System Output:-

File Output:-

No comments:
Write comments

Recommended Posts × +