File IO in c++ 1

Introduction This tutorial will start with the very basis of File I/O (Input/Output) in C++. After that, I will look into aspects that are more advanced, showing you some tricks, and describing useful functions. You need to have good understanding of C++, otherwise this tutorial will be unfamiliar and not useful to you! Your Very First Program I will first write the code, and after that, I will explain it line by line. [Read More]

Reading from file or Reading integers from file

#include
#include
#include
#include

using namespace std;

int main() {
    int sum = 0;
    int x;
    ifstream inFile;

    inFile.open(“integers.txt”); // read integers from text file
    if (!inFile) {
        cout « “Unable to open file”;
        exit(0); // terminate with error
    }

while (inFile » x) {
        sum = sum+x;
    }
    inFile.close();

cout « “Sum = " « sum « endl;
    return 0;
}