How to read a file in in C++
C++
// io/read-file-sum.cpp - Read integers from file and print sum.
// Fred Swartz 2003-08-20
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
int sum = 0;
int x;
ifstream inFile;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> x) {
sum = sum + x;
}
inFile.close();
cout << "Sum = " << sum << endl;
return 0;
}
Also in C++:
- Title
- c++ operator overloading not equal
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- max heap c++ stl;
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- clear file before writing c++
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- exponenciacion binaria
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- c++ for loops
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- how read a shader from another file c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- c++ passing two dimensional array to function
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- capitalize first letter c++
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- extends c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- How to traverse in a tree iterative C++
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- knapsack
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- c++ printf char as hex
- Category
- C++
- Title
- dfenwick tree code c++
- Category
- C++
- Title
- how to return a vector in c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++