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:-
Output:-
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
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:-
File Output:-
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
Such a nice blog, I got very much information and it is very useful for us. For More details, visit us.
ReplyDeletec++ read file line by line
Can you do it adding the values from text file column by column?
ReplyDelete