Answers for "open a file for writing and if it exists write content to end of file in c++"

C++
8

How to read a file in in 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;
}
Posted by: Guest on March-13-2020
0

write to file in C++

ofstream myfile;
myfile.open("file.txt");

myfile << "write this to file"
Posted by: Guest on July-01-2020

Code answers related to "open a file for writing and if it exists write content to end of file in c++"

Browse Popular Code Answers by Language